当前位置:网站首页>Unity给自己的脚本添加类似编辑器扩展的功能案例ContextMenu的使用
Unity给自己的脚本添加类似编辑器扩展的功能案例ContextMenu的使用
2022-07-04 17:40:00 【charlsdm】
给自己的物体附加名字最主要提升自己的开发效率,下边就举一个例子来说明一下,下边是放上我的代码
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("输入的NameKey不包含请重新输入");
}
else if(this.GetComponent<SpriteRenderer>()!=null)
{
if (GameResAssetBundle.GameResSprite.ContainsKey(NameKey))
this.GetComponent<SpriteRenderer>().sprite = GameResAssetBundle.GameResSprite[NameKey];
else
Debug.Log("输入的NameKey不包含请重新输入");
}
}
}
[ContextMenu("自动给我写的脚本添加名字")]
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("自动给我原本的图片赋值为空")]
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;
}
}
}
}
边栏推荐
- TorchDrug教程
- Rookie post station management system based on C language
- 2022 ByteDance daily practice experience (Tiktok)
- 建立自己的网站(15)
- 《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
- 【机器学习的数学基础】(一)线性代数(Linear Algebra)(上+)
- 【2022年江西省研究生数学建模】冰壶运动 思路分析及代码实现
- 千万不要只学 Oracle、MySQL!
- 从实时应用角度谈通信总线仲裁机制和网络流控
- [mathematical basis of machine learning] (I) linear algebra (Part 1 +)
猜你喜欢
随机推荐
C语言打印练习
信息学奥赛一本通 1336:【例3-1】找树根和孩子
Scala basic tutorial -- 17 -- Collection
每日一题(2022-07-02)——最低加油次数
Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg
2022健康展,北京健博会,中国健康展,大健康展11月13日
问下各位大佬有用过cdc直接mysql to clickhouse的么
Deleting nodes in binary search tree
Principle and application of ThreadLocal
完善的js事件委托
[cloud voice suggestion collection] cloud store renewal and upgrading: provide effective suggestions, win a large number of code beans, Huawei AI speaker 2!
神经网络物联网是什么意思通俗的解释
My colleagues quietly told me that flying Book notification can still play like this
Is the securities account opened by qiniu safe?
DeFi生态NFT流动性挖矿系统开发搭建
sqlserver的CDC第一次查询的能读取到数据,但后面增删改读取不到,是什么原因
6.26cf simulation match B: solution to array reduction problem
[2022 Jiangxi graduate mathematical modeling] curling movement idea analysis and code implementation
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转