当前位置:网站首页>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;
}
}
}
}
边栏推荐
猜你喜欢
How is the entered query SQL statement executed?
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
一种将Tree-LSTM的强化学习用于连接顺序选择的方法
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
Li Kou brush question diary /day8/7.1
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
. Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
How is the entered query SQL statement executed?
Lex and yacc based lexical analyzer + parser
Mxnet implementation of googlenet (parallel connection network)
随机推荐
《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
Scala基础教程--20--Akka
发送和接收IBM WebSphere MQ消息
Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg
Nature Microbiology | 可感染阿斯加德古菌的六种深海沉积物中的病毒基因组
性能优化之关键渲染路径
Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
Scala basic tutorial -- 19 -- actor
Scala basic tutorial -- 12 -- Reading and writing data
Perfect JS event delegation
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
力扣刷题日记/day1/2022.6.23
Interpretation of SIGMOD '22 hiengine paper
Scala basic tutorial -- 18 -- set (2)
C语言打印练习
一、C语言入门基础
Wireshark抓包TLS协议栏显示版本不一致问题
Caché WebSocket
神经网络物联网应用技术学什么
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