Unity Editor -Cubemap

一、创建我们的Editor脚本


using UnityEngine;
using UnityEditor;
public class ResizablePanels : EditorWindow
{
}

二、初始化窗口并且给标题起名

编辑器窗口需要初始化静态方法。 在这个方法中,我们将构建窗口并(可选)给它一个标题。 GetWindow()是一个EditorWindow方法,它创建窗口(如果它不存在),或者查找并关注它(如果存在)。


using UnityEngine;
using UnityEditor;
public class ResizablePanels : EditorWindow
{
[ MenuItem( "Window/Resizable Panels")]
private static void OpenWindow()
{
ResizablePanels window = GetWindow< ResizablePanels>();
window. titleContent = new GUIContent( "Resizable Panels");
}
}

三、采用OnGUI()创建两个矩形

为了在这个窗口中绘图,我们将使用OnGUI()方法(是的,Unity自己的编辑器系统仍然使用旧的GUI系统,它可能不会长时间改变)。但首先,我们需要两个矩形来定义我们的面板。我还将在他们自己的方法中绘制这些面板,所以当我们处理它时,我们也应该添加这些方法。


using UnityEngine;
using UnityEditor;
public class ResizablePanels : EditorWindow
{
private Rect upperPanel;
private Rect lowerPanel;
[ MenuItem( "Window/Resizable Panels")]
private static void OpenWindow()
{
ResizablePanels window = GetWindow< ResizablePanels>();
window. titleContent = new GUIContent( "Resizable Panels");
}
private void OnGUI()
{
DrawUpperPanel();
DrawLowerPanel();
}
private void DrawUpperPanel()
{
}
private void DrawLowerPanel()
{
}
}

我们的编辑窗口开始形成。我们现在需要做的就是绘制面板并检查它们是否正常工作。GUILayout.BeginArea(Rect rect)将创建一个要绘制的矩形区域,GUILayout.EndArea()标记结束。这些区域将定义我们的面板。我还要在这两个区域添加标签,以便我们可以看到它们的外观。


using UnityEngine;
using UnityEditor;
public class ResizablePanels : EditorWindow
{
private Rect upperPanel;
private Rect lowerPanel;
[ MenuItem( "Window/Resizable Panels")]
private static void OpenWindow()
{
ResizablePanels window = GetWindow< ResizablePanels>();
window. titleContent = new GUIContent( "Resizable Panels");
}
private void OnGUI()
{
DrawUpperPanel();
DrawLowerPanel();
}
private void DrawUpperPanel()
{
upperPanel = new Rect( 0, 0, position. width, position. height * 0.5f);
GUILayout. BeginArea( upperPanel);
GUILayout. Label( "Upper Panel");
GUILayout. EndArea();
}
private void DrawLowerPanel()
{
lowerPanel = new Rect( 0, position. height * 0.5f, position. width, position. height * 0.5f);
GUILayout. BeginArea( lowerPanel);
GUILayout. Label( "Lower Panel");
GUILayout. EndArea();
}
}

四、调节两个矩形的大小


using UnityEngine;
using UnityEditor;
public class ResizablePanels : EditorWindow
{
private Rect upperPanel;
private Rect lowerPanel;
private Rect resizer;
private float sizeRatio = 0.5f;
private bool isResizing;
private GUIStyle resizerStyle;
[ MenuItem( "Window/Resizable Panels")]
private static void OpenWindow()
{
ResizablePanels window = GetWindow< ResizablePanels>();
window. titleContent = new GUIContent( "Resizable Panels");
}
private void OnEnable()
{
resizerStyle = new GUIStyle();
resizerStyle. normal. background = EditorGUIUtility. Load( "icons/d_AvatarBlendBackground.png") as Texture2D;
}
private void OnGUI()
{
DrawUpperPanel();
DrawLowerPanel();
DrawResizer();
ProcessEvents( Event. current);
if ( GUI. changed) Repaint();
}
private void DrawUpperPanel()
{
upperPanel = new Rect( 0, 0, position. width, position. height * sizeRatio);
GUILayout. BeginArea( upperPanel);
GUILayout. Label( "Upper Panel");
GUILayout. EndArea();
}
private void DrawLowerPanel()
{
lowerPanel = new Rect( 0, ( position. height * sizeRatio) + 5, position. width, position. height * ( 1 - sizeRatio) - 5);
GUILayout. BeginArea( lowerPanel);
GUILayout. Label( "Lower Panel");
GUILayout. EndArea();
}
private void DrawResizer()
{
resizer = new Rect( 0, ( position. height * sizeRatio) - 5f, position. width, 10f);
GUILayout. BeginArea( new Rect( resizer. position + ( Vector2. up * 5f), new Vector2( position. width, 2)), resizerStyle);
GUILayout. EndArea();
EditorGUIUtility. AddCursorRect( resizer, MouseCursor. ResizeVertical);
}
private void ProcessEvents( Event e)
{
switch ( e. type)
{
case EventType. MouseDown:
if ( e. button == 0 && resizer. Contains( e. mousePosition))
{
isResizing = true;
}
break;
case EventType. MouseUp:
isResizing = false;
break;
}
Resize( e);
}
private void Resize( Event e)
{
if ( isResizing)
{
sizeRatio = e. mousePosition. y / position. height;
Repaint();
}
}
}

原文

————————————————————————————————————————

创建cubemap

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.Experimental.Rendering;

public class CMGenerator : EditorWindow
{
    [MenuItem("Window/Cubemap Generator")]
    static void Init(){

        CMGenerator window = (CMGenerator)EditorWindow.GetWindow(typeof(CMGenerator));
        window.Show();
    }
    const string defaultPath = "Assets/Cubemap/";
    private GameObject obj;
    private int width;
    string filename;
    void OnGUI(){
        obj = (GameObject)EditorGUILayout.ObjectField("target position",obj,typeof(GameObject),true);
        if(obj != null){
            GUILayout.Label(obj.transform.position.ToString());
        }else{
            GUILayout.Label("None Object Chosen");
        }
        width = EditorGUILayout.IntField("width",width);
        filename = EditorGUILayout.TextField("filename",filename);
        if(GUILayout.Button("create cubemap")){
            
            if(width > 0 && filename != ""){
                CreateCubemap(obj.transform.position,width,defaultPath,filename);
            }else{
                Debug.LogError("Missing width or filename");
            }
        }
        
    }
    void CreateCubemap(Vector3 position,int width,string path,string name){
        /* create a gameobejct at target position 
        and initialize a crmera C, create a cubemap through
        C's RenderToCubemap Function */

        Cubemap _cubemap = new Cubemap(width,DefaultFormat.LDR,TextureCreationFlags.None);
        GameObject _current = new GameObject();
        _current.transform.position = position;
        _current.AddComponent<Camera>();
        if(_current.GetComponent<Camera>().RenderToCubemap(_cubemap)){
            Debug.Log("generate successfully");
            GameObject.DestroyImmediate(_current);
            if(Directory.Exists(path)){
                AssetDatabase.CreateAsset(_cubemap,$"{path}{name}.cubemap");
            }else{
                Directory.CreateDirectory(path);
                AssetDatabase.CreateAsset(_cubemap,$"{path}{name}.cubemap");
            }
            AssetDatabase.SaveAssets();
        }else{
            Debug.Log("generate failed");
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值