当前位置:网站首页>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;
}
}
}
}
边栏推荐
- 2022养生展,健康展,北京大健康展,健康产业展11月举办
- How is the entered query SQL statement executed?
- .NET ORM框架HiSql实战-第二章-使用Hisql实现菜单管理(增删改查)
- 模板_判断素数_开方 / 六素数法
- Uni app and uviewui realize the imitation of Xiaomi mall app (with source code)
- 力扣刷题日记/day6/6.28
- 基于lex和yacc的词法分析器+语法分析器
- Using SSH
- 大佬们,求助一下,我用mysql cdc 2.2.1(flink 1.14.5)写入kafka,设置
- 力扣刷题日记/day7/2022.6.29
猜你喜欢
Li Kou brush question diary /day7/6.30
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
力扣刷题日记/day7/6.30
神经网络物联网平台搭建(物联网平台搭建实战教程)
一种将Tree-LSTM的强化学习用于连接顺序选择的方法
What types of Thawte wildcard SSL certificates provide
Scala basic tutorial -- 19 -- actor
激进技术派 vs 项目保守派的微服务架构之争
Microservice architecture debate between radical technologists vs Project conservatives
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
随机推荐
Scala基础教程--18--集合(二)
Li Kou brush question diary /day8/7.1
整理混乱的头文件,我用include what you use
Halcon模板匹配
2022 ByteDance daily practice experience (Tiktok)
[go ~ 0 to 1] read, write and create files on the sixth day
repeat_P1002 [NOIP2002 普及组] 过河卒_dp
激进技术派 vs 项目保守派的微服务架构之争
Process of manually encrypt the mass-producing firmware and programming ESP devices
vbs或vbe如何修改图标
A method of using tree LSTM reinforcement learning for connection sequence selection
Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg
[opencv introduction to mastery 9] opencv video capture, image and video conversion
Principle and application of ThreadLocal
From automation to digital twins, what can Tupo do?
Li Kou brush question diary /day1/2022.6.23
Li Kou brush question diary /day4/6.26
Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
redis分布式锁的8大坑总结梳理
正则替换【JS,正则表达式】