您的位置:首页 > 移动开发 > Unity3D

Unity菜单编程-自定义的图片Inspector面板

2016-06-22 20:06 1316 查看

本类以.png图片的inspector面板为例,实现一个图片自定义的Inspector面板,Inspector面板可以直接针对当前选中对象做一个操作。

下面的mytestInspector类是用于将当前选中的.png图片自动转为Sprite类型,但是我有一点不太清楚TextureImporter类在修改textureType的时候,如果没有跟着指定textureFormat图片格式的,就会出现白图片。重新指定一下图片格式就OK了

用到的方法:

OnEnable() 激活时调用,

OnInspectorGUI GUI刷新类似于mono的OnGUI(),所用的面板组件也和OnGUI差不多

不过学习写的过程中发现几个问题:

- 其中,myclass类可以是一个普通类,用来保存或响应Inspector菜单类中的设置或操作

- 继承于Editor的TextureImporter 菜单类不一定非放在Editor目录下

- 同类型的Inspector面板,比如同时有两TextureImporter类,个同时只会有一个生效,并且Editor目录下的那个菜单类优先生效

如图是编译完 图片属性面板的效果:



最后,附上源码:

using System;
using UnityEditor;
using UnityEngine;

/// </remarks>
/**
这一行比较关键 指明是图片导入菜单
**/
[CustomEditor(typeof(TextureImporter))]
public class mytestInspector : Editor
{
//inspector激活时调用,选中图片时也会会调用
public void OnEnable()
{
Type type = Type.GetType("UnityEditor.TextureImporterInspector, UnityEditor");

//选中图时 修改图片属性啊
TextureImporter import = (TextureImporter)target;
if (import.textureType != TextureImporterType.Sprite)
{
import.textureType = TextureImporterType.Sprite;
import.mipmapEnabled = false;
import.textureFormat = TextureImporterFormat.RGBA32;
import.filterMode = FilterMode.Bilinear;
import.SaveAndReimport();
import.textureFormat = TextureImporterFormat.AutomaticCompressed;
Debug.Log(Time.time + " focuse on cur image forse set Image as Sprite type!");
}
}

//inspector GUI
public override void OnInspectorGUI()
{
string assetPath = ((TextureImporter)target).assetPath;
//if (assetPath.EndsWith(".png"))
{
GUIContent testStr1 = new GUIContent("input testStr", "teststr to myclass");
myClass.testStr = EditorGUILayout.TextField(testStr1, myClass.testStr);

if (GUILayout.Button("test Button"))
{
myClass.testClick(assetPath);
}
}
}
}

public class myClass
{
public static string testStr = "";

public static void testClick(string assetPath)
{
//TODO
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity 编程