当前位置:网站首页>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;
}
}
}
}
边栏推荐
- Caché WebSocket
- Scala基础教程--15--递归
- 神经网络物联网是什么意思通俗的解释
- 2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import 'fmt' func
- Li Kou brush question diary /day7/2022.6.29
- 激进技术派 vs 项目保守派的微服务架构之争
- What types of Thawte wildcard SSL certificates provide
- VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
- 使用SSH
- Scala基础教程--14--隐式转换
猜你喜欢
随机推荐
Li Kou brush question diary /day7/6.30
发送和接收IBM WebSphere MQ消息
使用SSH
Microservice architecture debate between radical technologists vs Project conservatives
Go微服务(二)——Protobuf详细入门
模板_判断素数_开方 / 六素数法
DeFi生态NFT流动性挖矿系统开发搭建
力扣刷题日记/day3/2022.6.25
IBM WebSphere MQ检索邮件
资料下载 丨首届腾讯技术开放日课程精华!
【2022年江西省研究生数学建模】冰壶运动 思路分析及代码实现
Torchdrug tutorial
完善的js事件委托
力扣刷题日记/day8/7.1
C language printing exercise
[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors
中国农科院基因组所汪鸿儒课题组诚邀加入
Scala基础教程--14--隐式转换
Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
Li Kou brush question diary /day1/2022.6.23