当前位置:网站首页>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;
}
}
}
}
边栏推荐
- [发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0
- Li Kou brush question diary /day3/2022.6.25
- Detailed explanation of issues related to SSL certificate renewal
- [release] a tool for testing WebService and database connection - dbtest v1.0
- Using SSH
- 英特尔集成光电研究最新进展推动共封装光学和光互连技术进步
- Nebula Importer 数据导入实践
- C#实现定义一套中间SQL可以跨库执行的SQL语句(案例详解)
- repeat_P1002 [NOIP2002 普及组] 过河卒_dp
- 学习路之PHP--phpstudy创建项目时“hosts文件不存在或被阻止打开”
猜你喜欢

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

Use canal and rocketmq to listen to MySQL binlog logs

2022年字节跳动日常实习面经(抖音)

vbs或vbe如何修改图标

Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?

中国农科院基因组所汪鸿儒课题组诚邀加入

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

Nebula importer data import practice

Scala basic tutorial -- 20 -- akka

建立自己的网站(15)
随机推荐
ESP32-C3入门教程 问题篇⑫——undefined reference to rom_temp_to_power, in function phy_get_romfunc_addr
Li Kou brush question diary /day2/2022.6.24
Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
Interpretation of SIGMOD '22 hiengine paper
Using FTP
Using SSH
Technology sharing | interface testing value and system
基于C语言的菜鸟驿站管理系统
神经网络物联网是什么意思通俗的解释
MXNet对GoogLeNet的实现(并行连结网络)
[release] a tool for testing WebService and database connection - dbtest v1.0
php伪原创api对接方法
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
Li Kou brush question diary /day3/2022.6.25
Scala基础教程--17--集合
整理混乱的头文件,我用include what you use
Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?
Scala基础教程--16--泛型
Li Kou brush question diary /day4/6.26
Scala基础教程--19--Actor