首页 > 代码库 > UnityEditor扩展编辑器实现从场景中渲染得到Cubemap

UnityEditor扩展编辑器实现从场景中渲染得到Cubemap

(学习笔记,希望能帮助到有需要的人。)



在自定义的EditorWindow中定义2个变量,分别代表需要渲染的Cubemap 和 视点对象(通常是Camera对象)

private Cubemap cubemap;

private GameObject obj;


在OnGUI 函数中

<span style="white-space:pre">		</span>this.cubemap = (Cubemap) EditorGUILayout.ObjectField (this.cubemap, typeof(Cubemap), false);

		EditorGUILayout.Space ();

		if ( GUILayout.Button ("Select Camera Object in Scene")) {

			if (Selection.activeGameObject) {
				this.obj = Selection.activeGameObject;
			}
            
		}

		if (this.obj != null) {
			EditorGUILayout.HelpBox ("obj name : " + this.obj.name, MessageType.None);
		}

		EditorGUILayout.Space ();

		if (this.obj != null) {
			if (GUILayout.Button ("Render to Cubemap")) {

				this.renderToCubeMap();
			}
		}
从编辑窗口中得到 资源文件 Cubemap 和  Camera对象。  


在renderToCubeMap函数中

<span style="white-space:pre">		</span>if (this.cubemap != null && this.obj.camera != null) {

			this.obj.camera.RenderToCubemap(this.cubemap);
		}

这样就从camera所处的位置,渲染生成了6张纹理图,并生成立方体贴图Cubemap。


至此,就得到了从场景中定制得到的Cubemap。




下面是整体代码


using UnityEngine;
using UnityEditor;
using System.Collections;

public class GenerateCubeMapTool : EditorWindow {

	private Cubemap cubemap;

	private GameObject obj;

//	// Use this for initialization
//	void Start () {
//	}
	
	// Update is called once per frame
	void Update () {
	
	}

	[MenuItem("Tools/GenerateCubeMapTool")]
	static void Init () {
//		GenerateCubeMapTool window = (GenerateCubeMapTool) EditorWindow.GetWindow(typeof(GenerateCubeMapTool));
		EditorWindow.GetWindow (typeof(GenerateCubeMapTool));
	}

	void OnGUI () {

		EditorGUILayout.Space ();

		this.cubemap = (Cubemap) EditorGUILayout.ObjectField (this.cubemap, typeof(Cubemap), false);

		EditorGUILayout.Space ();

		if ( GUILayout.Button ("Select Camera Object in Scene")) {

			if (Selection.activeGameObject) {
				this.obj = Selection.activeGameObject;
			}
            
		}

		if (this.obj != null) {
			EditorGUILayout.HelpBox ("obj name : " + this.obj.name, MessageType.None);
		}

		EditorGUILayout.Space ();

		if (this.obj != null) {
			if (GUILayout.Button ("Render to Cubemap")) {

				this.renderToCubeMap();
			}
		}
	}


	void renderToCubeMap() {

		if (this.cubemap != null && this.obj.camera != null) {

			this.obj.camera.RenderToCubemap(this.cubemap);
		}
		else if (this.obj.camera == null) {
			Debug.Log("Please make sure the gameObject has the camera component");
		}
		else if (this.cubemap == null) {
			Debug.Log("Please select the cubemap, and try again.");
		}
	}
}



UnityEditor扩展编辑器实现从场景中渲染得到Cubemap