unity自定义Scene窗口。Scene窗口添加按钮等,如下效果
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using UnityEngine.Tilemaps;
using UnityEditor.Experimental;
using System.Reflection;
using System;
public class SceneViewEx : SceneView
{
[MenuItem("Test/SceneViewEx")]
static void SceneViewExShow()
{
EditorWindow.GetWindow<SceneViewEx>().Show();
Texture2D texture2D = InnerLoadGeneratedIconOrNormalIcon("SceneviewFx");
}
public override void OnEnable()
{
//getAttribute();
//OnEnableTest();
try
{
base.OnEnable();
}
catch
{
}
}
private void Awake()
{
AwakeInvoke();
//getAttribute();
}
private void AwakeInvoke()
{
MethodInfo methodInfo = typeof(SceneView).GetMethod("Awake", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.CreateInstance);
methodInfo.Invoke(this, null);
}
int mask;
float v;
Color color;
AnimationCurve animationCurve;
UnityEngine.Object gameObject;
private void OnGUI()
{
OnGUIInvoke();
WragUI();
}
private void WragUI()
{
value = EditorGUILayout.FloatField(value);
EditorGUILayout.LabelField("文本");
mask = EditorGUILayout.MaskField(new GUIContent { text = "测试" }, mask, new string[] { "test" });
v = EditorGUILayout.Slider(v, 0, 1);
color = EditorGUILayout.ColorField(new GUIContent { text = "颜色" }, color, true, true, true);
animationCurve = EditorGUILayout.CurveField(animationCurve);
EditorGUILayout.DropdownButton(new GUIContent { text = "DropdownButton" }, FocusType.Passive);
gameObject= EditorGUILayout.ObjectField(gameObject,gameObject.GetType(),true);
}
float value;
private void OnGUIInvoke()
{
MethodInfo methodInfo = typeof(SceneView).GetMethod("OnGUI", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.CreateInstance);
methodInfo.Invoke(this, null);
}
private void getAttribute()
{
Attribute[] attributes = new List<Attribute>( typeof(SceneView).GetCustomAttributes()).ToArray();
object[] attrs = typeof(SceneView).GetCustomAttributes(true);
foreach (Attribute attr in attrs)
{
Type typeTemp =attr.GetType();
CustomAttributeData[] dd=new List<CustomAttributeData>( attr.TypeId.GetType().CustomAttributes).ToArray();
if (typeTemp.Name== "EditorWindowTitleAttribute")
{
PropertyInfo propertyInfo = typeTemp.GetProperty("useTypeNameAsIconName");
propertyInfo.SetValue(attr,true);
PropertyInfo iconName = typeTemp.GetProperty("icon");
iconName.SetValue(attr, "SceneviewFx");
}
}
}
public string title { get; set; }
public string icon { get; set; }
public bool useTypeNameAsIconName { get; set; }
public override void OnDisable()
{
base.OnDisable();
}
public override void AddItemsToMenu(GenericMenu menu)
{
base.AddItemsToMenu(menu);
}
#region 测试代码
private void OnEnableTest()
{
GUIContent m_Lighting = EditorGUIUtility.TrIconContent("SceneviewLighting", "When toggled on, the Scene lighting is used. When toggled off, a light attached to the Scene view camera is used.");
GUIContent m_Fx = EditorGUIUtility.TrIconContent("SceneviewFx", "Toggle skybox, fog, and various other effects.");
GUIContent m_AudioPlayContent = EditorGUIUtility.TrIconContent("SceneviewAudio", "Toggle audio on or off.");
GUIContent m_GizmosContent = EditorGUIUtility.TrTextContent("Gizmos", "Toggle the visibility of different Gizmos in the Scene view.");
GUIContent m_2DModeContent = EditorGUIUtility.TrTextContent("2D", "When togggled on, the Scene is in 2D view. When toggled off, the Scene is in 3D view.");
GUIContent m_RenderDocContent = EditorGUIUtility.TrIconContent("renderdoc", "Capture the current view and open in RenderDoc.");
}
/// <summary>
/// 加载编辑器图片
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
static Texture2D InnerLoadGeneratedIconOrNormalIcon(string name)
{
Texture2D tex = EditorGUIUtility.Load(EditorResources.generatedIconsPath + name + ".asset") as Texture2D;
if (!tex)
{
tex = EditorGUIUtility.Load(EditorResources.iconsPath + name + ".png") as Texture2D;
}
if (!tex)
{
tex = EditorGUIUtility.Load(name) as Texture2D; // Allow users to specify their own project path to an icon (e.g see EditorWindowTitleAttribute)
}
return tex;
}
#endregion 测试代码
}
来源:CSDN
作者:创世界---
链接:https://blog.csdn.net/fengya1/article/details/88859164