当前位置:网站首页>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;
}
}
}
}
边栏推荐
- Caché JSON 使用JSON适配器
- repeat_P1002 [NOIP2002 普及组] 过河卒_dp
- Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
- Scala基础教程--16--泛型
- 读写关闭的channel是啥后果?
- Send and receive IBM WebSphere MQ messages
- 神经网络物联网平台搭建(物联网平台搭建实战教程)
- MXNet对GoogLeNet的实现(并行连结网络)
- 【2022年江西省研究生数学建模】水汽过饱和的核化除霾 思路分析及代码实现
- 发送和接收IBM WebSphere MQ消息
猜你喜欢

LeetCode第300场周赛(20220703)

字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验

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

Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
![[release] a tool for testing WebService and database connection - dbtest v1.0](/img/4e/4154fec22035725d6c7aecd3371b05.jpg)
[release] a tool for testing WebService and database connection - dbtest v1.0

What if the self incrementing ID of online MySQL is exhausted?

vbs或vbe如何修改图标

英特尔集成光电研究最新进展推动共封装光学和光互连技术进步

Nature Microbiology | 可感染阿斯加德古菌的六种深海沉积物中的病毒基因组

Scala基础教程--16--泛型
随机推荐
Download the first Tencent technology open day course essence!
资料下载 丨首届腾讯技术开放日课程精华!
从实时应用角度谈通信总线仲裁机制和网络流控
[go ~ 0 to 1] read, write and create files on the sixth day
My colleagues quietly told me that flying Book notification can still play like this
Principle and application of ThreadLocal
Li Kou brush question diary /day1/2022.6.23
物联网应用技术的就业前景和现状
Torchdrug tutorial
Go microservice (II) - detailed introduction to protobuf
ByteDance dev better technology salon was successfully held, and we joined hands with Huatai to share our experience in improving the efficiency of web research and development
《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
What if the self incrementing ID of online MySQL is exhausted?
问下各位大佬有用过cdc直接mysql to clickhouse的么
2022 ByteDance daily practice experience (Tiktok)
Scala basic tutorial -- 14 -- implicit conversion
How is the entered query SQL statement executed?
每日一题(2022-07-02)——最低加油次数
Don't just learn Oracle and MySQL!
Cache é JSON uses JSON adapters