编辑器扩展-改写URP模板的ReadMe

改写ReadMe文件

效果与元效果一致

在这里插入图片描述

元效果时读配置文件wtl 进行赋值

using System;
using UnityEngine;

//加入了 创建功能 不过扩展只会读取第一个
[CreateAssetMenu(fileName = "readme", menuName = "note", order =1)]
public class Readme : ScriptableObject {
    
    

	public Texture2D icon;
	
	public string title;
	
	public Section[] sections = new Section[] {
    
     };
	
	public bool loadedLayout;
	
	[Serializable]
	public class Section {
    
    
		public string heading, text, linkText, url;
	}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Reflection;

[CustomEditor(typeof(Readme))]
[InitializeOnLoad]
public class ReadmeEditor : Editor
{
    
    

	static string kShowedReadmeSessionStateName = "ReadmeEditor.showedReadme";

	static float kSpace = 16f;

	static ReadmeEditor()
	{
    
    
		EditorApplication.delayCall += SelectReadmeAutomatically;
	}

	static void SelectReadmeAutomatically()
	{
    
    
		if (!SessionState.GetBool(kShowedReadmeSessionStateName, false))
		{
    
    
			var readme = SelectReadme();
			SessionState.SetBool(kShowedReadmeSessionStateName, true);

			if (readme && !readme.loadedLayout)
			{
    
    
				LoadLayout();
				readme.loadedLayout = true;
			}
		}
	}

	static void LoadLayout()
	{
    
    
		var assembly = typeof(EditorApplication).Assembly;
		var windowLayoutType = assembly.GetType("UnityEditor.WindowLayout", true);
		var method = windowLayoutType.GetMethod("LoadWindowLayout", BindingFlags.Public | BindingFlags.Static);
		method.Invoke(null, new object[] {
    
     Path.Combine(Application.dataPath, "TutorialInfo/Layout.wlt"), false });
	}

	[MenuItem("Tutorial/Show Tutorial Instructions")]
	static Readme SelectReadme()
	{
    
    
		var ids = AssetDatabase.FindAssets("Readme t:Readme");
		if (ids.Length == 1)
		{
    
    
			var readmeObject = AssetDatabase.LoadMainAssetAtPath(AssetDatabase.GUIDToAssetPath(ids[0]));

			Selection.objects = new UnityEngine.Object[] {
    
     readmeObject };

			return (Readme)readmeObject;
		}
		else
		{
    
    
			Debug.Log("Couldn't find a readme");
			return null;
		}
	}

	protected override void OnHeaderGUI()
	{
    
    
		var readme = (Readme)target;
		Init();

		var iconWidth = Mathf.Min(EditorGUIUtility.currentViewWidth / 3f - 20f, 128f);

		GUILayout.BeginHorizontal("In BigTitle");
		{
    
    
			GUILayout.Label(readme.icon, GUILayout.Width(iconWidth), GUILayout.Height(iconWidth));
			GUILayout.Label(readme.title, TitleStyle);
		}
		GUILayout.EndHorizontal();
	}

	public override void OnInspectorGUI()
	{
    
    
		var readme = (Readme)target;
		Init();

		foreach (var section in readme.sections)
		{
    
    
			if (!string.IsNullOrEmpty(section.heading))
			{
    
    
				GUILayout.Label(section.heading, HeadingStyle);
			}
			if (!string.IsNullOrEmpty(section.text))
			{
    
    
				GUILayout.Label(section.text, BodyStyle);
			}
			if (!string.IsNullOrEmpty(section.linkText))
			{
    
    
				if (LinkLabel(new GUIContent(section.linkText)))
				{
    
    
					Application.OpenURL(section.url);
				}
			}
			GUILayout.Space(kSpace);
		}
	}


	bool m_Initialized;

	GUIStyle LinkStyle {
    
     get {
    
     return m_LinkStyle; } }
	[SerializeField] GUIStyle m_LinkStyle;

	GUIStyle TitleStyle {
    
     get {
    
     return m_TitleStyle; } }
	[SerializeField] GUIStyle m_TitleStyle;

	GUIStyle HeadingStyle {
    
     get {
    
     return m_HeadingStyle; } }
	[SerializeField] GUIStyle m_HeadingStyle;

	GUIStyle BodyStyle {
    
     get {
    
     return m_BodyStyle; } }
	[SerializeField] GUIStyle m_BodyStyle;

	void Init()
	{
    
    
		if (m_Initialized)
			return;
		m_BodyStyle = new GUIStyle(EditorStyles.label);
		m_BodyStyle.wordWrap = true;
		m_BodyStyle.fontSize = 14;

		m_TitleStyle = new GUIStyle(m_BodyStyle);
		m_TitleStyle.fontSize = 26;

		m_HeadingStyle = new GUIStyle(m_BodyStyle);
		m_HeadingStyle.fontSize = 18;

		m_LinkStyle = new GUIStyle(m_BodyStyle);
		m_LinkStyle.wordWrap = false;
		// Match selection color which works nicely for both light and dark skins
		m_LinkStyle.normal.textColor = new Color(0x00 / 255f, 0x78 / 255f, 0xDA / 255f, 1f);
		m_LinkStyle.stretchWidth = false;

		m_Initialized = true;
	}

	bool LinkLabel(GUIContent label, params GUILayoutOption[] options)
	{
    
    
		var position = GUILayoutUtility.GetRect(label, LinkStyle, options);

		Handles.BeginGUI();
		Handles.color = LinkStyle.normal.textColor;
		Handles.DrawLine(new Vector3(position.xMin, position.yMax), new Vector3(position.xMax, position.yMax));
		Handles.color = Color.white;
		Handles.EndGUI();

		EditorGUIUtility.AddCursorRect(position, MouseCursor.Link);

		return GUI.Button(position, label, LinkStyle);
	}
}

这里使用Editor改写赋值方式

  • 每一个创建的数据均可进行显示GUI
using UnityEngine;

[CreateAssetMenu(fileName = "ReadMe_FWC", menuName = "FWC_Note", order = 1)]
public class ReadMe_FWC : ScriptableObject
{
    
    
	public Texture2D icon;
	public string title;
}
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEngine;

/// <summary>
/// 改写自动GUI
/// </summary>
[CustomEditor(typeof(ReadMe_FWC))]
[InitializeOnLoad]
public class ReadMe_FWCEditor : Editor
{
    
    
	static string KEY_read = "ReadmeEditor.showedReadmeWFC";

	static ReadMe_FWCEditor()
	{
    
    
		//打开项目 根据储存的值自动展示 这里不是重点
		EditorApplication.delayCall += SelectReadmeAutomatically;
	}

	/// <summary>
	/// 自动选中readme展示
	/// </summary>
	static void SelectReadmeAutomatically()
	{
    
    
		SelectReadme();

		if (!SessionState.GetBool(KEY_read, false))
		{
    
    
			//选择此资源对象
			SelectReadme();

			//PlayerPrefer的编辑器版本
			SessionState.SetBool(KEY_read, true);
		}
	}

	/// <summary>
	/// 加载外部配置文件
	/// </summary>
	static void LoadLayout()
	{
    
    
		// class Assembly --程序集
		var assembly = typeof(EditorApplication).Assembly;

		// class Assembly { Type GetType(string name); } --类型
		var windowLayoutType = assembly.GetType("UnityEditor.WindowLayout", true);

		//class Type { MethodInfo GetMethod(string name, BindingFlags bindingAttr); } --方法名 --方法类型
		var method = windowLayoutType.GetMethod("LoadWindowLayout", BindingFlags.Public | BindingFlags.Static);

		//class MethodBase{ object Invoke(object obj, object[] parameters); } //绘制Layout
		method.Invoke(null, new object[] {
    
     Path.Combine(Application.dataPath, "TutorialInfo/Layout.wlt"), false });
	}

	[MenuItem("URP案例/选择改写文件")]
	static void SelectReadme()
	{
    
    
		//GUID 查找一组ReadMe类型的 名字=ReadMe的文件
		var ids = AssetDatabase.FindAssets("ReadMe_FWC t:ReadMe_FWC");

		if (ids.Length >= 1)
		{
    
    
			//转换GUID组的元素 => 路径
			string assetPath = AssetDatabase.GUIDToAssetPath(ids[0]);
			//加载对象 第一种方式 路径[内置指定类型 通过GUID]
			var readmeObject = AssetDatabase.LoadMainAssetAtPath(assetPath);
			//第二种方式 路径加类型
			//readmeObject = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Readme) );

			//选择对象
			Selection.objects = new Object[] {
    
     readmeObject };

		}
		else
		{
    
    
			Debug.Log("Couldn't find a readme");
		}
	}

	//-----------------------------------------------------

    #region var
    Texture icon;
	string title;

	/// <summary>
	/// 自定以内容类
	/// </summary>
	public class Section
	{
    
    
		public string heading, text, linkText, url;
	}

	Section[] s_arr = new Section[7];

	Section s0 = new Section()
	{
    
    
		heading = "Universal Render Pipeline",
		text = "The Universal Project Template configures Project settings for Projects where performance, wide platform support, and ease of customizing graphics are the primary considerations.",
		linkText = "",
		url = "",
	};
	Section s1 = new Section()
	{
    
    
		heading = "",
		text = "This Template uses the Universal Render Pipeline (URP) and Shader Graph.",
		linkText = "",
		url = "",
	};
	Section s2 = new Section()
	{
    
    
		heading = "",
		text = "URP is prebuilt Scriptable Render Pipeline that is quick and easy to customize, and lets you create optimized graphics across a wide range of platforms. URP also includes an optimized 2D renderer complete with 2D lights and pixel perfect rendering, and an integrated post-processing solution.",
		linkText = "",
		url = "",
	};
	Section s3 = new Section()
	{
    
    
		heading = "",
		text = "Shader Graph is a tool that allows you to create shaders using a visual node editor instead of writing code.",
		linkText = "",
		url = "",
	};
	Section s4 = new Section()
	{
    
    
		heading = "",
		text = "This template contains a sample Scene that contains examples of how to configure lighting settings, Materials, Shaders, and post-processing effects in URP, several preconfigured Universal Render Pipeline Assets that let you quickly swap between graphics quality levels, and Presets that have been optimized for use with URP.",
		linkText = "",
		url = "",
	};
	Section s5 = new Section()
	{
    
    
		heading = "",
		text = "To read more about URP and its built-in features, see the",
		linkText = "URP documentation",
		url = "https://docs.unity3d.com/Packages/com.unity.render-pipelines.universal@latest/index.html",
	};
	Section s6 = new Section()
	{
    
    
		heading = "",
		text = "For more information about Shader Graph, see the",
		linkText = "Shader Graph documentation",
		url = "https://docs.unity3d.com/Packages/com.unity.shadergraph@latest",
	};
	#endregion

	private void OnEnable()
	{
    
    
		icon = Resources.Load<Texture2D>("UniversalIcon");
		title = "Universal Render Pipeline Template";

		s_arr = new Section[7] {
    
     s0, s1, s2, s3, s4, s5, s6 };
	}

	/// <summary>
	/// 重写标题格式
	/// </summary>
	protected override void OnHeaderGUI()
	{
    
    
		Init();

		//icon位置 左上
		var iconWidth = Mathf.Min(EditorGUIUtility.currentViewWidth / 3f - 20f, 128f);

		//顶层 横排显示 === 显示自定义内容
		GUILayout.BeginHorizontal("In BigTitle");
		{
    
    
			GUILayout.Label(icon, GUILayout.Width(iconWidth), GUILayout.Height(iconWidth));
			GUILayout.Label(title, TitleStyle);
		}
		GUILayout.EndHorizontal();
	}

	public override void OnInspectorGUI()
	{
    
    
		Init();

		foreach (var section in s_arr)
		{
    
    
			if (!string.IsNullOrEmpty(section.heading))
			{
    
    
				GUILayout.Label(section.heading, HeadingStyle);
			}
			if (!string.IsNullOrEmpty(section.text))
			{
    
    
				GUILayout.Label(section.text, BodyStyle);
			}
			if (!string.IsNullOrEmpty(section.linkText))
			{
    
    
				if (LinkLabel(new GUIContent(section.linkText)))
				{
    
    
					Application.OpenURL(section.url);
				}
			}
			GUILayout.Space(16);
		}
	}


    #region Style

    bool m_Initialized;

	GUIStyle LinkStyle {
    
     get {
    
     return m_LinkStyle; } }
	[SerializeField] GUIStyle m_LinkStyle;

	GUIStyle TitleStyle {
    
     get {
    
     return m_TitleStyle; } }
	[SerializeField] GUIStyle m_TitleStyle;

	GUIStyle HeadingStyle {
    
     get {
    
     return m_HeadingStyle; } }
	[SerializeField] GUIStyle m_HeadingStyle;

	GUIStyle BodyStyle {
    
     get {
    
     return m_BodyStyle; } }
	[SerializeField] GUIStyle m_BodyStyle;

	/// <summary>
	/// 初始化GUI样式表
	/// </summary>
	void Init()
	{
    
    
		if (m_Initialized)
			return;

		m_BodyStyle = new GUIStyle(EditorStyles.label);
		m_BodyStyle.wordWrap = true;
		m_BodyStyle.fontSize = 14;

		m_TitleStyle = new GUIStyle(m_BodyStyle);
		m_TitleStyle.fontSize = 26;

		m_HeadingStyle = new GUIStyle(m_BodyStyle);
		m_HeadingStyle.fontSize = 18;

		m_LinkStyle = new GUIStyle(m_BodyStyle);
		m_LinkStyle.wordWrap = false;

		// Match selection color which works nicely for both light and dark skins
		m_LinkStyle.normal.textColor = new Color(0x00 / 255f, 0x78 / 255f, 0xDA / 255f, 1f);
		m_LinkStyle.stretchWidth = false;

		m_Initialized = true;
	}

	/// <summary>
	/// 跳转链接文字组件
	/// </summary>
	/// <param name="label"></param>
	/// <param name="options"></param>
	/// <returns></returns>
	bool LinkLabel(GUIContent label, params GUILayoutOption[] options)
	{
    
    
		var position = GUILayoutUtility.GetRect(label, LinkStyle, options);

		Handles.BeginGUI();
		Handles.color = LinkStyle.normal.textColor;
		Handles.DrawLine(new Vector3(position.xMin, position.yMax), new Vector3(position.xMax, position.yMax));
		Handles.color = Color.white;
		Handles.EndGUI();

		EditorGUIUtility.AddCursorRect(position, MouseCursor.Link);

		return GUI.Button(position, label, LinkStyle);
	}

	#endregion

}

包的链接如下:

链接

猜你喜欢

转载自blog.csdn.net/weixin_38531633/article/details/118735431