当前位置:网站首页>Unity adds a function case similar to editor extension to its script, the use of ContextMenu
Unity adds a function case similar to editor extension to its script, the use of ContextMenu
2022-07-04 19:20:00 【charlsdm】
Adding names to your objects is the most important thing to improve your development efficiency , The following is an example to illustrate , Next is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NameKeyValueString : MonoBehaviour
{
public string NameKey = "null";
public Sprite MySprite = null;
// Start is called before the first frame update
void Start()
{
if(this.GetComponent<Image>()!=null||this.GetComponent<SpriteRenderer>()!=null)
{
if(this.GetComponent<Image>()!=null)
{
if (GameResAssetBundle.GameResSprite.ContainsKey(NameKey))
this.GetComponent<Image>().sprite = GameResAssetBundle.GameResSprite[NameKey];
else
Debug.Log(" Input NameKey Not included, please re-enter ");
}
else if(this.GetComponent<SpriteRenderer>()!=null)
{
if (GameResAssetBundle.GameResSprite.ContainsKey(NameKey))
this.GetComponent<SpriteRenderer>().sprite = GameResAssetBundle.GameResSprite[NameKey];
else
Debug.Log(" Input NameKey Not included, please re-enter ");
}
}
}
[ContextMenu(" Automatically add a name to the script I write ")]
public void AddNameKeyMyName()
{
string myname = null;
if(this.GetComponent<Image>()!=null||this.GetComponent<SpriteRenderer>()!=null)
{
if(this.GetComponent<Image>()!=null)
{
myname = this.GetComponent<Image>().sprite.name;
}
else if(this.GetComponent<Image>()!=null)
{
myname = this.GetComponent<SpriteRenderer>().sprite.name;
}
}
if(myname!=null)
{
NameKey = myname;
}
}
[ContextMenu(" Automatically assign null to my original image ")]
public void ClearPictureOrSpriteOrigionName()
{
string myname = null;
if (this.GetComponent<Image>() != null || this.GetComponent<SpriteRenderer>() != null)
{
if (this.GetComponent<Image>() != null)
{
myname = this.GetComponent<Image>().sprite.name;
}
else if (this.GetComponent<Image>() != null)
{
myname = this.GetComponent<SpriteRenderer>().sprite.name;
}
}
if (myname != null)
{
if (this.GetComponent<Image>() != null)
{
this.GetComponent<Image>().sprite = null;
}
else if (this.GetComponent<Image>() != null)
{
this.GetComponent<SpriteRenderer>().sprite = null;
}
}
}
}
边栏推荐
- One question per day (2022-07-02) - Minimum refueling times
- Torchdrug tutorial
- Using FTP
- How to open an account is safe,
- 输入的查询SQL语句,是如何执行的?
- C语言打印练习
- C # implementation defines a set of SQL statements that can be executed across databases in the middle of SQL (detailed explanation of the case)
- Scala基础教程--18--集合(二)
- redis分布式锁的8大坑总结梳理
- 完善的js事件委托
猜你喜欢

一种将Tree-LSTM的强化学习用于连接顺序选择的方法

神经网络物联网是什么意思通俗的解释

Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project

Scala basic tutorial -- 20 -- akka

每日一题(2022-07-02)——最低加油次数

【uniapp】uniapp开发app在线预览pdf文件

Nebula Importer 数据导入实践

使用canal配合rocketmq监听mysql的binlog日志

性能优化之关键渲染路径

Scala basic tutorial -- 18 -- set (2)
随机推荐
How is the entered query SQL statement executed?
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
TorchDrug教程
资料下载 丨首届腾讯技术开放日课程精华!
【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转
One question per day (2022-07-02) - Minimum refueling times
使用FTP
Scala基础教程--20--Akka
SSL证书续费相关问题详解
Scala基础教程--14--隐式转换
Scala basic tutorial -- 19 -- actor
整理混乱的头文件,我用include what you use
模板_判断素数_开方 / 六素数法
Scala基础教程--19--Actor
自由小兵儿
Li Kou brush question diary /day2/2022.6.24
从实时应用角度谈通信总线仲裁机制和网络流控
Download the first Tencent technology open day course essence!
Scala basic tutorial -- 18 -- set (2)
Scala基础教程--12--读写数据