当前位置:网站首页>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;
}
}
}
}
边栏推荐
猜你喜欢
随机推荐
大佬们,求助一下,我用mysql cdc 2.2.1(flink 1.14.5)写入kafka,设置
Basic tutorial of scala -- 16 -- generics
Lex and yacc based lexical analyzer + parser
发送和接收IBM WebSphere MQ消息
2022年字节跳动日常实习面经(抖音)
【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转
Pb extended DLL development (super chapter) (VII)
基于C语言的菜鸟驿站管理系统
2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import 'fmt' func
C语言打印练习
How to open an account is safe,
What types of Thawte wildcard SSL certificates provide
李迟2022年6月工作生活总结
更安全、更智能、更精致,长安Lumin完虐宏光MINI EV?
Mxnet implementation of googlenet (parallel connection network)
Wireshark抓包TLS协议栏显示版本不一致问题
Scala基础教程--16--泛型
sqlserver的CDC第一次查询的能读取到数据,但后面增删改读取不到,是什么原因
基于lex和yacc的词法分析器+语法分析器
Detailed explanation of issues related to SSL certificate renewal