当前位置:网站首页>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;
}
}
}
}
边栏推荐
猜你喜欢
My colleagues quietly told me that flying Book notification can still play like this
基于unity的愤怒的小鸟设计
MXNet对GoogLeNet的实现(并行连结网络)
整理混乱的头文件,我用include what you use
Lex and yacc based lexical analyzer + parser
Li Kou brush question diary /day3/2022.6.25
千万不要只学 Oracle、MySQL!
A method of using tree LSTM reinforcement learning for connection sequence selection
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
Rookie post station management system based on C language
随机推荐
Wanghongru research group of Institute of genomics, Chinese Academy of Agricultural Sciences is cordially invited to join
2021 合肥市信息学竞赛小学组
Unity编辑器扩展C#遍历文件夹以及子目录下的所有图片
神经网络物联网应用技术就业前景【欢迎补充】
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import 'fmt' func
LeetCode第300场周赛(20220703)
正则替换【JS,正则表达式】
Wireshark抓包TLS协议栏显示版本不一致问题
【uniapp】uniapp开发app在线预览pdf文件
Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
A method of using tree LSTM reinforcement learning for connection sequence selection
请教一下 flinksql中 除了数据统计结果是状态被保存 数据本身也是状态吗
Scala basic tutorial -- 19 -- actor
[opencv introduction to mastery 9] opencv video capture, image and video conversion
更安全、更智能、更精致,长安Lumin完虐宏光MINI EV?
C语言打印练习
Mxnet implementation of googlenet (parallel connection network)
每日一题(2022-07-02)——最低加油次数
Caché WebSocket
Summary and sorting of 8 pits of redis distributed lock