Unity 自動製作LowPoly樹預製體工具

通過一個樹幹模型和樹葉模型 隨機制作不同的預製體

Editor腳本:

//=====================================================
// - FileName:      AutoCreateTreeModel.cs
// - Author:       #AuthorName#
// - CreateTime:    #CreateTime#
// - Email:         #AuthorEmail#
// - Description:   
// -  (C) Copyright 2019, webeye,Inc.
// -  All Rights Reserved.
//======================================================
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Qarth;
using Random = UnityEngine.Random;
using Object = UnityEngine.Object;

namespace GameWish.Game
{
    public class AutoCreateTreeModel
    {
        static int UpLevel = 1;
        static int MidLevel = 3;
        static int LowLevel = 2;

        static float midR=1.5f;
        static float topR = 0.1f;
        static float botR = 1f;

        static float lowHighIntevel = 0.4f;

        [MenuItem("Tools/CreateTreePrefab %#J")]
        private static void CreateTreePrefab()
        {
            GameObject go = PrefabUtility.InstantiatePrefab(Resources.Load("TreeCreateModel/scene_tree_model"))as GameObject;
            Transform root = go.transform.GetChild(0).Find("LeaveRoot");
            GameObject model = null;
            if (!go)
            {
                Log.e("Select is null!Please select a treeModel");
                return;
            }
            go.IterateGameObject((child)=> 
            {
                if (child.name.Contains("leave_"))
                {
                    GameObject.DestroyImmediate(child);
                }
            });

            if (root)
            {
                model = root.Find("leave").gameObject;
            }
            if (model)
            {
                //樹的中段(空心圓生成)
                for (int i = 0; i < MidLevel; i++)
                {
                    GameObject clone1 = GameObject.Instantiate(model);
                    clone1.SetActive(true);
                    clone1.name = string.Format("leave_mid_{0}",i);
                    clone1.transform.SetParent(root);
                    Vector2 p = Random.insideUnitCircle * midR;
                    clone1.transform.localPosition = new Vector3(p.x, 0, p.y);

                }
                //樹的下段
                for (int i = 0; i < LowLevel; i++)
                {
                    GameObject clone2 = GameObject.Instantiate(model);
                    clone2.SetActive(true);
                    clone2.name = string.Format("leave_bot_{0}", i);
                    clone2.transform.SetParent(root);
                    Vector2 p = Random.insideUnitCircle * botR;
                    clone2.transform.localPosition = new Vector3(p.x, -lowHighIntevel, p.y);

                }
                //樹的上段
                for (int i = 0; i < UpLevel; i++)
                {
                    GameObject clone3 = GameObject.Instantiate(model);
                    clone3.SetActive(true);
                    clone3.name = string.Format("leave_top_{0}", i);
                    clone3.transform.SetParent(root);
                    Vector2 p = Random.insideUnitCircle * topR;
                    clone3.transform.localPosition = new Vector3(p.x, lowHighIntevel, p.y);
                }
                model.SetActive(false);
            }
            else
            {
                Log.e("選中的物體{0}無法制作樹木模型!", go.name);
                return;
            }

            foreach (Transform child in root)
            {
                child.transform.localScale *= Random.Range(0.9f, 1.2f);
                //child.transform.Rotate(new Vector3(RandomHelper.Range(0, 360), RandomHelper.Range(0, 360), RandomHelper.Range(0, 360)));
            }
            go.IterateGameObject((child) =>
            {
                child.isStatic = true;
            });

            go.name = string.Format("auto_tree_{0}",System.DateTime.Now.ToString("mmddss"));
            //製作預製體到對應路徑
            bool success = false;
            Object tempPre = PrefabUtility.SaveAsPrefabAsset(go,string.Format("Assets/Res/FolderMode/Prefabs/AutoTree/{0}.prefab", go.name),out success);
            if (success)
            {
                Log.i("Create {0} Prefab Successful!!", go.name);
            }
            else
            {
                Log.e("Create {0} Prefab Failed!!", go.name);
            }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章