当前位置:网站首页>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;
}
}
}
}
边栏推荐
- C language printing exercise
- 2022 ByteDance daily practice experience (Tiktok)
- 模板_判断素数_开方 / 六素数法
- 1、 Introduction to C language
- Send and receive IBM WebSphere MQ messages
- 李迟2022年6月工作生活总结
- 2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import 'fmt' func
- . Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
- Caché JSON 使用JSON适配器
- 删除字符串中出现次数最少的字符【JS,Map排序,正则】
猜你喜欢

每日一题(2022-07-02)——最低加油次数

建立自己的网站(15)

力扣刷题日记/day8/7.1

1、 Introduction to C language

Wanghongru research group of Institute of genomics, Chinese Academy of Agricultural Sciences is cordially invited to join

Li Kou brush question diary /day8/7.1

力扣刷题日记/day3/2022.6.25

从实时应用角度谈通信总线仲裁机制和网络流控

基于C语言的菜鸟驿站管理系统

My colleagues quietly told me that flying Book notification can still play like this
随机推荐
神经网络物联网平台搭建(物联网平台搭建实战教程)
SSL证书续费相关问题详解
Li Kou brush question diary /day3/2022.6.25
一、C语言入门基础
How is the entered query SQL statement executed?
A method of using tree LSTM reinforcement learning for connection sequence selection
Caché WebSocket
Scala basic tutorial -- 12 -- Reading and writing data
【2022年江西省研究生数学建模】水汽过饱和的核化除霾 思路分析及代码实现
力扣刷题日记/day4/6.26
力扣刷題日記/day6/6.28
Scala基础教程--19--Actor
Scala基础教程--12--读写数据
Scala基础教程--13--函数进阶
Li Kou brush question diary /day8/7.1
Process of manually encrypt the mass-producing firmware and programming ESP devices
How to download files using WGet and curl
Scala basic tutorial -- 17 -- Collection
力扣刷题日记/day7/2022.6.29
Li Kou brush question diary /day2/2022.6.24