Unity的改进预览之"LOD组和样条曲线路径"

Unity启动了一个叫”NinjaCamp”的项目,旨在快速的扩展改进Unity,集大家的智慧,任意发挥,迸发出新的创意想法,大项目,小项目,或者几乎不可能的项目.择优保留.

http://player.youku.com/player.php/sid/XMjU5OTg3NTky/v.swf

一个新的组件,LOD Group,可以很方便的进行模型多个精度的自动优化切换.比如按照当前模型在屏幕上显示的尺寸来选择需要显示的模型精度.具体看视频吧.

另外这个可以拿模型进行类似放样的操作,做自己想要的道路什么的很方便.

http://player.youku.com/player.php/sid/XMjU5OTczOTA0/v.swf

转载请注明来自1Vr.Cn
原文:http://blogs.unity3d.com/2011/03/29/ninjacamp-iii-spine-roads/http://blogs.unity3d.com/2011/03/28/ninjacamp-iii-lod-groups/

Unity的改进预览之"快速预设"

Unity启动了一个叫”NinjaCamp”的项目,旨在快速的扩展改进Unity,集大家的智慧,任意发挥,迸发出新的创意想法,大项目,小项目,或者几乎不可能的项目.择优保留.

原本要创建一个预设Prefabs,需要先新建一个空的Prefab,再拖拽需要的东西到这个预设上.这次的改进是只需将游戏对象拖拽到项目视图中就可以快速创建一个预设.省时省力.具体看视频吧.

http://player.youku.com/player.php/sid/XMjU5OTY2NTAw/v.swf

转载请注明来自1Vr.Cn
原文:http://blogs.unity3d.com/2011/03/25/ninjacamp-iii-quick-prefabs/

Unity的改进预览之"Firefox与Ubuntu"

Unity启动了一个叫”NinjaCamp”的项目,旨在快速的扩展改进Unity,集大家的智慧,任意发挥,迸发出新的创意想法,大项目,小项目,或者几乎不可能的项目.择优保留.

通过Mozilla的开发人员参考,OpenGL的文献,X11的文件以及若干类似红牛的饮料,钻研了几天之后我们小有成果.

这是在Ubuntu10.10上的Firefox 4.0上运行Unity的效果.尽管Ff4.0还没有发布,但我们决定用它新的4.0API来做一些方便的事情.

不过目前这还是很粗糙,甚至不能在同一个浏览器中刷新重载.连带进度条的启动画面都没有,然后我们做到这个.我们很高兴!

转载请注明来自1Vr.Cn
原文:http://blogs.unity3d.com/2011/03/24/ninjacamp-iii-foxes-and-penguins-unite/

Unity的改进预览之"顶点着色绘图"?

Unity启动了一个叫”NinjaCamp”的项目,旨在快速的扩展改进Unity,集大家的智慧,任意发挥,迸发出新的创意想法,大项目,小项目,或者几乎不可能的项目.择优保留.

这个功能是顶点着色,视频里用来画柏油路的裂痕很酷嗷.

视频:

http://player.youku.com/player.php/sid/XMjU5OTkwMzA0/v.swf

Unity的改进预览之"怎么查找资源所属"?

Unity启动了一个叫”NinjaCamp”的项目,旨在快速的扩展改进Unity,集大家的智慧,任意发挥,迸发出新的创意想法,大项目,小项目,或者几乎不可能的项目.择优保留.

这个功能比较小,但是应该是相当有用.

我们添加了一个新的菜单叫”Filter References in Scene”,在层次视图中选择一个资源并且选择这项菜单的时候将在场景中高亮显示所有用这个资源的游戏对象,另外也可以手动输入”ref:ASSET PATH”命令来达到同
样的效果.

还会添加一项”Filter References in Project”菜单,以便帮助找到其他资源,比如用到这个资源的预设,场景等等.

转载请注明来自1Vr.Cn
原文:http://blogs.unity3d.com/2011/03/24/ninjacamp-iii-what-references-your-assets/

Unity的改进预览之"新建脚本对话框"

Unity启动了一个叫”NinjaCamp”的项目,旨在快速的扩展改进Unity,集大家的智慧,任意发挥,迸发出新的创意想法,择优保留.

下面我们介绍的是新建脚本对话框功能.目的是改进现在新建脚本的流程.如下图:

可以看到,可以在所选的目录中创建新的脚本.也可以重命名脚本,如果是C#脚本,重命名的时候类名也会随之改变.也能很方便的讲脚本赋予游戏对象.

当你选择添加脚本,这个窗口就会出现并且将悬浮在Unity最上方.这时候可以单击任何游戏对象或者预设将脚本附加过去,也可以点击项目面板任意文件夹来选择保存该脚本的地方.也可以选择你需要的回调函数,免去手写的麻烦.

上面这个图里的想法添加一个更明显方便的方法给添加组件功能.当然.这个还在开发中.

转载请注明来自1Vr.Cn.
原文:http://blogs.unity3d.com/2011/03/23/ninjacamp-iii-new-script-dialog

Unity烘焙材质到贴图脚本

这个脚本可以将复杂的材质,比如有法线贴图的材质进行”烘焙”,转变为单一的贴图,可用来将Unity的游戏移植到移动平台时候使用.

将这个脚本放Editor文件夹里,使用时选择一个Material材质,然后在菜单种”Custom/Bake Material”打开并调整照明和其他参数,点击Bake按钮,就会生成一个单一的贴图.

脚本:BakeMaterial.js
class BakeMaterialSettings
{
    private static var kEditorPrefsName = "BakeMaterialSettings";
    
    static var kBakingLayerShouldBeUnusedInScene = 30;
    static var kStandardTexNames = new Array ("_MainTex", "_BumpMap", "_Detail", "_ParallaxMap", "_Parallax");

    var bakeAlpha = false;
    var bakeMainTexAsWhite = false;
    var minTextureResolution = 8;
    var maxTextureResolution = 2048;

    var emptyScene = false;
    var useCustomLights = false;
    var ambient = Color.black;
    
    static var kLights = 3;
    var enableLight = new boolean[kLights];
    var colorLight = new Color[kLights];
    var dirLight = new Vector2[kLights];
    
    function BakeMaterialSettings ()
    {
        Load ();
    }
    
    function Load ()
    {
        bakeAlpha = EditorPrefs.GetBool(kEditorPrefsName + ".bakeAlpha");
        bakeMainTexAsWhite = EditorPrefs.GetBool(kEditorPrefsName + ".bakeMainTexAsWhite");
        minTextureResolution = EditorPrefs.GetInt(kEditorPrefsName + ".minTextureResolution", 8);
        maxTextureResolution = EditorPrefs.GetInt(kEditorPrefsName + ".maxTextureResolution", 2048);

        emptyScene = EditorPrefs.GetBool(kEditorPrefsName + ".emptyScene");
        useCustomLights = EditorPrefs.GetBool(kEditorPrefsName + ".useCustomLights");
        ambient.r = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.r");
        ambient.g = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.g");
        ambient.b = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.b");
        ambient.a = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.a", 1.0f);
        
        for (var q = 0; q < kLights; ++q)
        {
            enableLight[q] = EditorPrefs.GetBool(kEditorPrefsName + ".enableLight" + q);
            colorLight[q].r = EditorPrefs.GetFloat(kEditorPrefsName + ".color.r" + q, 0.5f);
            colorLight[q].g = EditorPrefs.GetFloat(kEditorPrefsName + ".color.g" + q, 0.5f);
            colorLight[q].b = EditorPrefs.GetFloat(kEditorPrefsName + ".color.b" + q, 0.5f);
            colorLight[q].a = EditorPrefs.GetFloat(kEditorPrefsName + ".color.a" + q, 1.0f);
            dirLight[q].x = EditorPrefs.GetFloat(kEditorPrefsName + ".dir.x" + q);
            dirLight[q].y = EditorPrefs.GetFloat(kEditorPrefsName + ".dir.y" + q);
        }
    }
    
    function Save ()
    {
        EditorPrefs.SetBool(kEditorPrefsName + ".bakeAlpha", bakeAlpha);
        EditorPrefs.SetBool(kEditorPrefsName + ".bakeMainTexAsWhite", bakeMainTexAsWhite);
        EditorPrefs.SetInt(kEditorPrefsName + ".minTextureResolution", minTextureResolution);
        EditorPrefs.SetInt(kEditorPrefsName + ".maxTextureResolution", maxTextureResolution);

        EditorPrefs.GetBool(kEditorPrefsName + ".emptyScene", emptyScene);
        EditorPrefs.SetBool(kEditorPrefsName + ".useCustomLights", useCustomLights);
        EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.r", ambient.r);
        EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.g", ambient.g);
        EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.b", ambient.b);
        EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.a", ambient.a);

        for (var q = 0; q < kLights; ++q)
        {
            EditorPrefs.SetBool(kEditorPrefsName + ".enableLight" + q, enableLight[q]);
            EditorPrefs.SetFloat(kEditorPrefsName + ".color.r" + q, colorLight[q].r);
            EditorPrefs.SetFloat(kEditorPrefsName + ".color.g" + q, colorLight[q].g);
            EditorPrefs.SetFloat(kEditorPrefsName + ".color.b" + q, colorLight[q].b);
            EditorPrefs.SetFloat(kEditorPrefsName + ".color.a" + q, colorLight[q].a);
            EditorPrefs.SetFloat(kEditorPrefsName + ".dir.x" + q, dirLight[q].x);
            EditorPrefs.SetFloat(kEditorPrefsName + ".dir.y" + q, dirLight[q].y);
        }
    }
}

class BakeMaterial extends EditorWindow
{
    private static var kMateriBakeNodeName = "__MateriaBakeSetup";
    private static var kWindowMinSize = Vector2 (300, 386);
    
    private static var settings : BakeMaterialSettings;
    private static var visible : boolean = false;
    
    private var camera : GameObject;
    private var plane : GameObject;
    private var previewTexture : Texture;
    private var lights : GameObject[] = new GameObject[BakeMaterialSettings.kLights];
    private var stateChanged = false;
    
    private var texViewScrollPosition = Vector2.zero;
    private var lastMaterial : Material;
    
    private var originalScene = "";
    
    private var scheduleBakeOnNextUpdate = false;

    
    private function SetupScene ()
    {
        DestroyScene ();
        var oldGo = GameObject.Find(kMateriBakeNodeName);
        if (oldGo)
            DestroyImmediate (oldGo);
        camera = new GameObject (kMateriBakeNodeName, Camera);
        plane = GameObject.CreatePrimitive (PrimitiveType.Plane);

        var cam = camera;
        cam.camera.backgroundColor = Color.black;
        cam.camera.clearFlags = CameraClearFlags.SolidColor;
        cam.camera.orthographic = true;
        cam.camera.orthographicSize = 5.0;
        cam.camera.cullingMask = 1 << settings.kBakingLayerShouldBeUnusedInScene;
        
        plane.transform.parent = cam.transform;
        plane.transform.position = Vector3.forward * 10.0;
        plane.transform.rotation = Quaternion.Euler (0, 0, 180) * Quaternion.Euler (-90, 0, 0);
        plane.layer = settings.kBakingLayerShouldBeUnusedInScene;
        
        for (var l in lights)
        {
            l = new GameObject ("Light", Light);
            l.light.type = LightType.Directional;
            l.light.cullingMask = 1 << settings.kBakingLayerShouldBeUnusedInScene;
            l.transform.parent = cam.transform;
            l.active = false;
        }
    }
    
    private function UpdateScene (m : Material)
    {
        for (q = 0; q < settings.kLights; ++q)
        {
            lights[q].active = settings.useCustomLights & settings.enableLight[q];
            lights[q].light.color = settings.colorLight[q];
            lights[q].transform.rotation =
                Quaternion.AngleAxis(settings.dirLight[q].x, Vector3.up) *
                Quaternion.AngleAxis(settings.dirLight[q].y, Vector3.right);
        }
        
        if (settings.useCustomLights)
            RenderSettings.ambientLight = settings.ambient;
        else if (settings.emptyScene)
            RenderSettings.ambientLight = Color.white;
            
        plane.renderer.material = m;
    }
        
    private function DestroyScene ()
    {
        GameObject.DestroyImmediate (camera);
        GameObject.DestroyImmediate (plane);
        GameObject.DestroyImmediate (previewTexture);
    }

    function UpdateMaterialPreview (m : Material) : RenderTexture
    {
        if (!m)
            return;
        
        var saveAmbientLight = RenderSettings.ambientLight;
        var saveMainTexture = m.mainTexture;
        if (settings.bakeMainTexAsWhite)
            m.mainTexture = null;
    
        
        // setup
        if (!camera)
            SetupScene ();
        camera.SetActiveRecursively(true);
        UpdateScene (m);
        
        var res = FindLargestTextureResolution (plane.renderer.sharedMaterial, settings.minTextureResolution, settings.maxTextureResolution);
        var rt = RenderCameraToRenderTexture (camera.camera, res.x, res.y);
        
        // restore
        camera.SetActiveRecursively(false);
        RenderSettings.ambientLight = saveAmbientLight;
        m.mainTexture = saveMainTexture;
        
        previewTexture = rt;
        return rt;
    }
    
    function CaptureMaterial(m : Material)
    {
        var matAssetPath = AssetDatabase.GetAssetPath (m);
        var assetPath = System.IO.Path.Combine (System.IO.Path.GetDirectoryName (matAssetPath), System.IO.Path.GetFileNameWithoutExtension (matAssetPath));

        var rt = UpdateMaterialPreview (m);
        RenderTextureToPNG (rt, settings.bakeAlpha, assetPath + ".png");
    }

    function OnEnable ()
    {
        if (!settings)
            settings = new BakeMaterialSettings ();
        SetupScene ();
        visible = true;
    }
    
    function OnDisable ()
    {
        DestroyScene ();
        settings.Save ();
        visible = false;
    }

    static function GetTargetMaterial () : Material
    {
        return EditorUtility.InstanceIDToObject (Selection.activeInstanceID) as Material;
    }

    function OnSelectionChange ()
    {
        Repaint ();
    }

    function Update ()
    {
        var rebuildScene = false;
        if (scheduleBakeOnNextUpdate)
        {
            Bake ();
            scheduleBakeOnNextUpdate = false;
            rebuildScene = true;
        }
        
        if (originalScene == "" && EditorApplication.currentScene == "")
            settings.emptyScene = true;
            
        if (settings.emptyScene && originalScene == "" && EditorApplication.currentScene != "")
        {
            DestroyScene ();
            if (EditorApplication.SaveCurrentSceneIfUserWantsTo ())
            {
                originalScene = EditorApplication.currentScene;
                EditorApplication.NewScene ();
            }
            else
                settings.emptyScene = false;
            rebuildScene = true;            
        }
        else if (!settings.emptyScene && originalScene != "")
        {
            EditorApplication.OpenScene (originalScene);
            rebuildScene = true;
            originalScene = "";
        }
        
        if (rebuildScene)
        {
            SetupScene ();
        }
        
        if (rebuildScene || stateChanged || !settings.emptyScene)
        {
            UpdateMaterialPreview (lastMaterial);
            Repaint ();
            stateChanged = false;
        }
    }
    
    function OnGUI ()
    {
        var material = GetTargetMaterial ();
        if (lastMaterial != material)
            UpdateMaterialPreview (material);
        if (material)
            lastMaterial = material;
        
        EditorGUILayout.BeginHorizontal();
            EditorGUILayout.BeginVertical(GUILayout.MaxWidth(200));
                if (!(originalScene == "" && EditorApplication.currentScene == ""))
                {
                    settings.emptyScene = !EditorGUILayout.BeginToggleGroup("Scene ligthing", !settings.emptyScene);
                    EditorGUILayout.EndToggleGroup();
                }
                settings.useCustomLights = EditorGUILayout.BeginToggleGroup("Custom lighting", settings.useCustomLights);
                if (settings.useCustomLights)
                {
                    EditorGUI.indentLevel = 1;
                    settings.ambient = EditorGUILayout.ColorField("Ambient", settings.ambient);
                    for (var q = 0; q < settings.kLights; ++q)
                    {
                        settings.enableLight[q] = EditorGUILayout.BeginToggleGroup("Light", settings.enableLight[q]);
                        EditorGUI.indentLevel = 2;
                            settings.colorLight[q] = EditorGUILayout.ColorField("Color", settings.colorLight[q]);
                            settings.dirLight[q] = EditorGUILayout.Vector2Field("Direction", settings.dirLight[q]);
                        EditorGUILayout.EndToggleGroup();
                    }
                }
                EditorGUI.indentLevel = 0;
                EditorGUILayout.EndToggleGroup();
                
                settings.bakeAlpha = EditorGUILayout.Toggle("Bake Alpha", settings.bakeAlpha);
                settings.bakeMainTexAsWhite = !EditorGUILayout.Toggle("MainTex", !settings.bakeMainTexAsWhite);
                settings.minTextureResolution = EditorGUILayout.IntField("Min Resolution", settings.minTextureResolution);
                settings.maxTextureResolution = EditorGUILayout.IntField("Max Resolution", settings.maxTextureResolution);
                settings.minTextureResolution = Mathf.Max(2, settings.minTextureResolution);
                settings.maxTextureResolution = Mathf.Max(settings.minTextureResolution, settings.maxTextureResolution);

                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Bake"))
                {
                    CaptureMaterial (lastMaterial);
                }
                if (GUILayout.Button("Bake Selected"))
                {
                    scheduleBakeOnNextUpdate = true;
                }
                EditorGUILayout.EndHorizontal();
                
            EditorGUILayout.EndVertical();
            
            texViewScrollPosition = EditorGUILayout.BeginScrollView (texViewScrollPosition);
                var r = GUILayoutUtility.GetAspectRect(1.0f);
                if (previewTexture)
                    EditorGUI.DrawPreviewTexture(r, previewTexture);
            EditorGUILayout.EndScrollView();        
        EditorGUILayout.EndHorizontal();
        
        if (GUI.changed)
        {
            stateChanged = true;
        }
    }
    
    @MenuItem("Custom/Bake Material ...", false, 5)
    static function CreateBakeEditor()
    {
        var window = EditorWindow.GetWindow(BakeMaterial);
        window.title = "Bake Material";
        window.minSize = kWindowMinSize;
        window.Show();
    }

    @MenuItem("Custom/Bake Selected Materials", false, 4)
    static function Bake()
    {
        var instanceIDs = Selection.instanceIDs;
        var currentScene = EditorApplication.currentScene;
    
        var wasAlreadyVisible = BakeMaterial.visible;
        var window = EditorWindow.GetWindow(BakeMaterial);
        
        if (window.settings.emptyScene)
        {
            if (!EditorApplication.SaveCurrentSceneIfUserWantsTo ())
                return;
            EditorApplication.NewScene ();
        }
        
        window.SetupScene ();
        for (var i in instanceIDs)
        {
            var m : Material = EditorUtility.InstanceIDToObject (i) as Material;
            if (m)
                window.CaptureMaterial (m);
        }
        window.DestroyScene ();
        
        if (window.settings.emptyScene && currentScene)
        {
            EditorApplication.OpenScene (currentScene);
        }
        
        if (!wasAlreadyVisible)
            window.Close ();
    }
    
    static function FindLargestTextureResolution (m : Material, minTexRes : int, maxTexRes : int) : Vector2
    {
        var res = Vector2 (minTexRes, minTexRes);
        for (var n in BakeMaterialSettings.kStandardTexNames)
        {
            if (!m.HasProperty (n))
                continue;
        
            var t : Texture = m.GetTexture (n);
            if (!t)
                continue;
            
            res.x = Mathf.Max (res.x, t.width);
            res.y = Mathf.Max (res.y, t.height);
        }
        res.x = Mathf.Min (res.x, maxTexRes);
        res.y = Mathf.Min (res.y, maxTexRes);
        return res;
    }
    
    static function RenderCameraToRenderTexture (cam : Camera, width : int, height : int) : RenderTexture
    {
        var rt = cam.camera.targetTexture;
        if (rt && rt.width != width && rt.height != height)
            DestroyImmediate(rt);
        if (!rt)
            rt = new RenderTexture (width, height, 24);
        cam.camera.targetTexture = rt;
        cam.camera.Render ();
        return rt;
    }
    
    static function RenderTextureToPNG (rt : RenderTexture, bakeAlpha : boolean, assetPath : String)
    {
        RenderTexture.active = rt;
        
        var screenShot = new Texture2D (rt.width, rt.height, bakeAlpha? TextureFormat.ARGB32 : TextureFormat.RGB24, false);
        screenShot.ReadPixels (Rect (0, 0, rt.width, rt.height), 0, 0);
        
        RenderTexture.active = null;
        
        var bytes = screenShot.EncodeToPNG ();
        System.IO.File.WriteAllBytes (assetPath, bytes);
        
        AssetDatabase.ImportAsset (assetPath, ImportAssetOptions.ForceUpdate);
    }
}

Unity中从目前选择对象创建预设(Prefab)脚本

在Unity中选择一个游戏对象,然后通过这个脚本来快速生成一个Prefab预设.
将脚本放在Editor文件夹中,选择游戏对象,支持多选,然后选择菜单GameObject > Create Prefab From Selection,就会在根目录生成一个预设,名字和选择的游戏对象一样.当已经存在同名的预设,将会提示是否覆盖.

脚本名:CreatePrefabFromSelected.cs

using UnityEditor;
using UnityEngine;
using System.Collections;

class CreatePrefabFromSelected : ScriptableObject
{
    const string menuTitle = "GameObject/Create Prefab From Selected";

    /// <summary>
    /// Creates a prefab from the selected game object.
    /// </summary>
    [MenuItem(menuTitle)]
    static void CreatePrefab()
    {
        GameObject[] obj = Selection.gameObjects;

        foreach (GameObject go in obj)
        {
            string name = go.name;
            string localPath = "Assets/" + name + ".prefab";

            if (AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject)))
            {
                if (EditorUtility.DisplayDialog("Are you sure?", "The prefab already exists. Do you want to overwrite it?", "Yes", "No"))
                {
                    createNew(go, localPath);
                }
            }
            else
            {
                createNew(go, localPath);
            }

        }
    }

    static void createNew(GameObject obj, string localPath)
    {
        Object prefab = EditorUtility.CreateEmptyPrefab(localPath);
        EditorUtility.ReplacePrefab(obj, prefab);
        AssetDatabase.Refresh();

        DestroyImmediate(obj);
        GameObject clone = EditorUtility.InstantiatePrefab(prefab) as GameObject;
    }

    /// <summary>
    /// Validates the menu.
    /// </summary>
    /// <remarks>The item will be disabled if no game object is selected.</remarks>
    [MenuItem(menuTitle, true)]
    static bool ValidateCreatePrefab()
    {
        return Selection.activeGameObject != null;
    }
}

原文:http://www.unifycommunity.com/wiki/index.php?title=CreatePrefabFromSelected