Exemplo n.º 1
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;
        }
    }
Exemplo n.º 2
0
    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();
    }