当前位置:网站首页>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;
}
}
}
}
边栏推荐
- 【uniapp】uniapp开发app在线预览pdf文件
- 力扣刷题日记/day6/6.28
- Principle and application of ThreadLocal
- 基于unity的愤怒的小鸟设计
- Mxnet implementation of googlenet (parallel connection network)
- 力扣刷题日记/day3/2022.6.25
- 删除字符串中出现次数最少的字符【JS,Map排序,正则】
- 2022 ByteDance daily practice experience (Tiktok)
- 神经网络物联网应用技术就业前景【欢迎补充】
- 力扣刷题日记/day1/2022.6.23
猜你喜欢

Scala basic tutorial -- 13 -- advanced function

Scala basic tutorial -- 20 -- akka
redis分布式锁的8大坑总结梳理

How is the entered query SQL statement executed?

TorchDrug教程

MXNet对GoogLeNet的实现(并行连结网络)

力扣刷题日记/day5/2022.6.27

【2022年江西省研究生数学建模】冰壶运动 思路分析及代码实现

Journal des problèmes de brosse à boutons de force / day6 / 6.28

整理混乱的头文件,我用include what you use
随机推荐
Build your own website (15)
性能优化之关键渲染路径
Li Kou brush question diary /day6/6.28
Uni app and uviewui realize the imitation of Xiaomi mall app (with source code)
Scala basic tutorial -- 18 -- set (2)
使用SSH
Scala basic tutorial -- 20 -- akka
Scala基础教程--17--集合
ThreadLocal原理与使用
其他InterSystems %Net工具
Go微服务(二)——Protobuf详细入门
How is the entered query SQL statement executed?
每日一题(2022-07-02)——最低加油次数
Rookie post station management system based on C language
千万不要只学 Oracle、MySQL!
力扣刷题日记/day2/2022.6.24
正则替换【JS,正则表达式】
工厂从自动化到数字孪生,图扑能干什么?
Download the first Tencent technology open day course essence!
. Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)