示例#1
0
    private static void SpawnDecalInstanceAt(Transform root, Material mat, DecalItemSet dis)
    {
        for (var i = 0; i < dis.DecalItemList.Count; i++)
        {
            DecalItemSet.DecalItem decalItem = dis.DecalItemList[i];

            if (!decalItem.DecalInstancePrefab)
            {
                continue;
            }
            GameObject go = Instantiate(decalItem.DecalInstancePrefab);

            // 先放到 root 下面,设置 local 值。
            go.transform.parent           = root;
            go.transform.localPosition    = decalItem.LocalPositionInRoot;
            go.transform.localEulerAngles = decalItem.LocalEulerAngleInRoot;
            go.transform.localScale       = Vector3.one;

            // 设置正确的 panret
            Transform targetParent = root.Find(decalItem.ParentPath);
            if (targetParent)
            {
                go.transform.parent = targetParent;
                go.name             = DecalInstanceName;
            }
            else
            {
                go.name = NoParentDecalInstanceName;
            }

            go.GetComponent <MeshRenderer>().sharedMaterial = mat;
        }
    }
示例#2
0
    private void InitDecalItemSetForIf2()
    {
        var dis = (DecalItemSet)target;

        dis.DecalItemList.Clear();

        for (int i = 0; i < RectArr.Length; i++)
        {
            DecalItemSet.DecalItem di = new DecalItemSet.DecalItem();
            di.DecalInstancePrefab   = null;
            di.ParentPath            = "";
            di.LocalPositionInRoot   = Vector3.zero;
            di.LocalEulerAngleInRoot = Vector3.zero;
            di.LocalScaleInRoot      = new Vector3(RectArr[i].width / RectArr[i].height, 1, 1);

            di.TargetObjPath = "";

            di.UvArea       = new Rect(RectArr[i].x / 1024, RectArr[i].y / 1024, RectArr[i].width / 1024, RectArr[i].height / 1024);
            di.PushDistance = .01f;

            dis.DecalItemList.Add(di);
        }

        EditorUtility.SetDirty(dis);
    }
示例#3
0
    private static void SpawnDecalAt(Transform root, Material mat, DecalItemSet dis)
    {
        for (var i = 0; i < dis.DecalItemList.Count; i++)
        {
            DecalItemSet.DecalItem decalItem = dis.DecalItemList[i];

            OoSimpleDecal decal = CreateDecal();
            decal.AutoUpdateMesh = true;
            decal.TargetObjects  = new[] { root.Find(decalItem.TargetObjPath).gameObject };
            decal.UvArea         = decalItem.UvArea;
            decal.PushDistance   = decalItem.PushDistance;

            // 先放到 root 下面,设置 local 值。
            decal.transform.parent           = root;
            decal.transform.localPosition    = decalItem.LocalPositionInRoot;
            decal.transform.localEulerAngles = decalItem.LocalEulerAngleInRoot;
            decal.transform.localScale       = decalItem.LocalScaleInRoot;

            // 设置正确的 panret
            Transform targetParent = root.Find(decalItem.ParentPath);
            if (targetParent)
            {
                decal.transform.parent = targetParent;
                decal.gameObject.name  = DecalName;
            }
            else
            {
                decal.gameObject.name = NoParentDecalName;
            }

            decal.DecalMaterial = mat;
        }
    }
示例#4
0
    public void SpawnDecalInstance()
    {
//		Debug.Assert(dis.DecalItemList.Count == matList.Count);

        for (var i = 0; i < Set.DecalItemList.Count; i++)
        {
            DecalItemSet.DecalItem decalItem = Set.DecalItemList[i];

            if (!decalItem.DecalInstancePrefab)
            {
                continue;
            }

            GameObject go = Instantiate(decalItem.DecalInstancePrefab, transform, true);

            // 先放到 root 下面,设置 local 值。
            go.transform.localPosition    = decalItem.LocalPositionInRoot;
            go.transform.localEulerAngles = decalItem.LocalEulerAngleInRoot;
            go.transform.localScale       = Vector3.one;

            // 设置正确的 parent
            Transform targetParent = transform.Find(decalItem.ParentPath);
            if (targetParent)
            {
                go.transform.parent = targetParent;
                go.name             = DecalInstanceName + " " + i;
            }
            else
            {
                go.name = NoParentDecalInstanceName + " " + i;
            }

            if (i < MatList.Count)
            {
                go.GetComponent <MeshRenderer>().sharedMaterial = MatList[i];
            }
        }
    }
    private void GenerateAndSaveDecalInstance()
    {
        // thisPath: Assets/BigMarch/OoSimpleDecalExample/Decal_ii-example.asset
        string thisPath = AssetDatabase.GetAssetPath(_decalApply.Set);
        // folderPath: Assets/BigMarch/OoSimpleDecalExample/Decal_ii-example/
        string saveFolderPath = thisPath.Replace(".asset", "/");

        if (!Directory.Exists(saveFolderPath))
        {
            Debug.LogError("无法存储,找不到同名的文件夹 " + saveFolderPath);
            return;
        }

        DecalItemSet dis = _decalApply.Set;

        dis.DecalItemList.Clear();

        Transform root = _decalApply.transform;

        List <GameObject> decalList = FindAllChild(root, DecalName);

        for (var i = 0; i < decalList.Count; i++)
        {
            EditorUtility.DisplayProgressBar("", "saving", i * 1f / decalList.Count);

            GameObject    decalObj = decalList[i];
            OoSimpleDecal decal    = decalObj.GetComponent <OoSimpleDecal>();

            // 创建 instance
            GameObject decalInstance = decal.CreateDecalInstance();

            // 储存 mesh
            Mesh meshToSave = decalInstance.GetComponent <MeshFilter>().sharedMesh;
            MeshUtility.Optimize(meshToSave);
            string meshName = dis.name + "-DecalMesh-" + i;
            AssetDatabase.CreateAsset(meshToSave, saveFolderPath + meshName + ".asset");

            // 重新关联 mesh
            Mesh loadMesh = AssetDatabase.LoadAssetAtPath <Mesh>(saveFolderPath + meshName + ".asset");
            decalInstance.GetComponent <MeshFilter>().sharedMesh = loadMesh;

            // 存储 prefab
            string     prefabName          = dis.name + "-DecalInstance-" + i;
            GameObject decalInstancePrefab =
                PrefabUtility.CreatePrefab(saveFolderPath + prefabName + ".prefab",
                                           decalInstance);

            // 删除 instance
            DestroyImmediate(decalInstance);

            // 记录本次循环的 decal item。
            DecalItemSet.DecalItem di = new DecalItemSet.DecalItem();
            di.DecalInstancePrefab = decalInstancePrefab;

            // parent 的 local 值
            di.ParentPath = GetTransformPath(root, decal.transform.parent);

            // 放到 root 下面记录 local 值。
            Transform cacheParent = decalObj.transform.parent;
            decalObj.transform.parent = root;
            di.LocalPositionInRoot    = decalObj.transform.localPosition;
            di.LocalEulerAngleInRoot  = decalObj.transform.localEulerAngles;
            di.LocalScaleInRoot       = decalObj.transform.localScale;

            // 放到原本的 parent 下面。
            decalObj.transform.parent = cacheParent;

            di.TargetObjPath = GetTransformPath(root, decal.TargetObjects[0].transform);

            di.UvArea       = decal.UvArea;
            di.PushDistance = decal.PushDistance;

            dis.DecalItemList.Add(di);
        }

        EditorUtility.ClearProgressBar();
        EditorUtility.SetDirty(dis);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        Repaint();
    }