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.");
		}
	}
}



要在 Unity 编辑器扩展中随机选择场景中的对象,你可以使用以下方法: 1. 使用 Object.FindObjectsOfType() 方法获取当前场景中的所有对象,并将其存储到一个数组中。 2. 使用 Random.Range() 方法生成一个随机数,用它来选择数组中的一个随机索引。 3. 使用所选索引来获取数组中的随机对象。 这里是一个示例代码,它在 Unity 编辑器中创建了一个按钮,点击该按钮将随机选择场景中的一个 Cube 对象并将其设置为选中状态: ```csharp using UnityEditor; using UnityEngine; public class RandomObjectSelector : EditorWindow { [MenuItem("Window/Random Object Selector")] static void OpenWindow() { RandomObjectSelector window = GetWindow<RandomObjectSelector>(); window.Show(); } void OnGUI() { if (GUILayout.Button("Select Random Object")) { // 获取场景中所有的 GameObject 对象 GameObject[] objects = Object.FindObjectsOfType<GameObject>(); // 生成一个随机索引并获取随机对象 int randomIndex = Random.Range(0, objects.Length); GameObject randomObject = objects[randomIndex]; // 设置随机对象为选中状态 Selection.activeGameObject = randomObject; } } } ``` 请注意,这个示例只是一个基本的概念演示,你需要根据自己的场景和需求进行修改和优化。同时,这个示例中使用了 Unity 编辑器窗口来创建按钮,你可以将这个代码添加到你的自定义编辑器窗口中,或者将代码添加到自定义菜单项或工具栏按钮的回调函数中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值