当前位置:网站首页>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;
}
}
}
}
边栏推荐
- 一种将Tree-LSTM的强化学习用于连接顺序选择的方法
- 1、 Introduction to C language
- IBM WebSphere MQ检索邮件
- Is Guoyuan futures a regular platform? Is it safe to open an account in Guoyuan futures?
- What if the self incrementing ID of online MySQL is exhausted?
- repeat_P1002 [NOIP2002 普及组] 过河卒_dp
- Scala basic tutorial -- 12 -- Reading and writing data
- sqlserver的CDC第一次查询的能读取到数据,但后面增删改读取不到,是什么原因
- Torchdrug tutorial
- Mxnet implementation of googlenet (parallel connection network)
猜你喜欢
随机推荐
Wireshark抓包TLS协议栏显示版本不一致问题
使用canal配合rocketmq监听mysql的binlog日志
redis分布式锁的8大坑总结梳理
How to open an account is safe,
建立自己的网站(15)
利用策略模式优化if代码【策略模式】
876. 链表的中间结点
2022养生展,健康展,北京大健康展,健康产业展11月举办
发送和接收IBM WebSphere MQ消息
Summary and sorting of 8 pits of redis distributed lock
Scala基础教程--18--集合(二)
Scala基础教程--20--Akka
C # implementation defines a set of SQL statements that can be executed across databases in the middle of SQL (detailed explanation of the case)
TorchDrug教程
Scala基础教程--15--递归
Uni app and uviewui realize the imitation of Xiaomi mall app (with source code)
中国农科院基因组所汪鸿儒课题组诚邀加入
C language printing exercise
1、 Introduction to C language
信息学奥赛一本通 1336:【例3-1】找树根和孩子