当前位置:网站首页>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;
}
}
}
}
边栏推荐
- 【机器学习的数学基础】(一)线性代数(Linear Algebra)(上+)
- LeetCode FizzBuzz C#解答
- 模板_判断素数_开方 / 六素数法
- 6.26CF模拟赛B:数组缩减题解
- Interpretation of SIGMOD '22 hiengine paper
- 更安全、更智能、更精致,长安Lumin完虐宏光MINI EV?
- Scala basic tutorial -- 17 -- Collection
- One question per day (2022-07-02) - Minimum refueling times
- Scala basic tutorial -- 18 -- set (2)
- 1672. 最富有客户的资产总量
猜你喜欢

Scala basic tutorial -- 18 -- set (2)

Scala基础教程--20--Akka

小发猫物联网平台搭建与应用模型

Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines

1、 Introduction to C language
![[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0](/img/4e/4154fec22035725d6c7aecd3371b05.jpg)
[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0

LeetCode第300场周赛(20220703)
redis分布式锁的8大坑总结梳理

How to modify icons in VBS or VBE
Summary and sorting of 8 pits of redis distributed lock
随机推荐
Build your own website (15)
神经网络物联网是什么意思通俗的解释
2022年字节跳动日常实习面经(抖音)
激进技术派 vs 项目保守派的微服务架构之争
File processing examples of fopen, FREAD, fwrite, fseek
Caché JSON 使用JSON适配器
Technology sharing | interface testing value and system
【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转
性能优化之关键渲染路径
C # implementation defines a set of SQL statements that can be executed across databases in the middle of SQL (detailed explanation of the case)
Scala基础教程--18--集合(二)
Send and receive IBM WebSphere MQ messages
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
发送和接收IBM WebSphere MQ消息
2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import 'fmt' func
小发猫物联网平台搭建与应用模型
Caché WebSocket
Is the securities account opened by qiniu safe?
Other InterSystems%net tools
Scala basic tutorial -- 17 -- Collection