Пример #1
0
    public static void createCopyBrush(bool destroySourceTiles)
    {
        MAP_tileFunctions.checkTileSelectionStatus();

        if (MAP_Editor.currentBrushType != eBrushTypes.copyBrush && MAP_Editor.selectTiles.Count > 0)
        {
            MAP_Editor.currentBrushType = eBrushTypes.copyBrush;

            if (MAP_Editor.brushTile != null)
            {
                DestroyImmediate(MAP_Editor.brushTile);
            }

            if (MAP_Editor.findTileMapParent())
            {
                Undo.RegisterFullObjectHierarchyUndo(MAP_Editor.tileMapParent, "Create Brush");

                MAP_Editor.brushTile = new GameObject();
                MAP_Editor.brushTile.transform.eulerAngles = new Vector3(MAP_Editor.tileRotationX, MAP_Editor.tileRotation, 0f);
                MAP_Editor.brushTile.transform.parent      = MAP_Editor.tileMapParent.transform;
                MAP_Editor.brushTile.name = "YuME_brushTile";

                MAP_Editor.brushTile.transform.position = MAP_Editor.selectTiles[0].transform.position;

                MAP_Editor.tileChildObjects.Clear();

                foreach (GameObject tile in MAP_Editor.selectTiles)
                {
#if UNITY_2018_3_OR_NEWER
                    GameObject tempTile = (GameObject)PrefabUtility.InstantiatePrefab(PrefabUtility.GetCorrespondingObjectFromSource(tile) as GameObject);
#else
                    GameObject tempTile = (GameObject)PrefabUtility.InstantiatePrefab(PrefabUtility.GetPrefabParent(tile) as GameObject);
#endif
                    tempTile.transform.parent      = MAP_Editor.brushTile.transform;
                    tempTile.transform.position    = tile.transform.position;
                    tempTile.transform.eulerAngles = tile.transform.eulerAngles;
                    tempTile.transform.localScale  = tile.transform.localScale;

                    MAP_Editor.tileChildObjects.Add(tempTile);

                    if (destroySourceTiles)
                    {
                        DestroyImmediate(tile);
                    }
                }

                MAP_Editor.selectTiles.Clear();

                MAP_Editor.showWireBrush = false;
            }
        }
    }
Пример #2
0
    public static void createCustomBrush(string customBrushName)
    {
        subMeshCount = 0;

        MAP_tileFunctions.checkTileSelectionStatus();

        GameObject tileParentObject = new GameObject();

        tileParentObject.AddComponent <MAP_tileGizmo>();
        tileParentObject.GetComponent <MAP_tileGizmo>().customBrushMeshName = new List <string>();

        string customBrushGUID = Guid.NewGuid().ToString("N");

        tileParentObject.name = customBrushName + Define.MAP_PREFAB;

        string destinationPath = MAPTools_Utils.getAssetsPath(MAP_Editor.availableTileSets[MAP_Editor.currentTileSetIndex]) + Define.CUSTOM_BRUSHFES + "/";

        if (MAP_Editor.selectTiles.Count > 0)
        {
            // When creating a custom brush we need to find the lowest Z transform in the selection to become the pivot transform
            GameObject bottomLevelOfSelection = MAP_Editor.selectTiles[0];

            foreach (GameObject checkObjects in MAP_Editor.selectTiles)
            {
                if (checkObjects.transform.position.z < bottomLevelOfSelection.transform.position.z)
                {
                    bottomLevelOfSelection = checkObjects;
                }
            }

            // center the parent object around the lowest block to make sure all the selected brushes are centered around the parent
            tileParentObject.transform.position = bottomLevelOfSelection.transform.position;

            // New Custom Brush implementation. Uses a technique similar to the freeze map, except for selected tils

            List <GameObject> tilesToCombine = new List <GameObject>();
            List <Transform>  _freezeTiles   = new List <Transform>();
            List <Transform>  freezeTiles    = new List <Transform>();

            foreach (GameObject tile in MAP_Editor.selectTiles)
            {
                tile.GetComponentsInChildren <Transform>(false, _freezeTiles);
                freezeTiles.AddRange(_freezeTiles);
            }

            foreach (Transform tile in freezeTiles)
            {
                if (tile.GetComponent <MeshRenderer>())
                {
                    tilesToCombine.Add(tile.gameObject);
                }
            }

            tilesToCombine = tilesToCombine.OrderBy(x => x.GetComponent <MeshRenderer>().sharedMaterial.name).ToList();

            Material previousMaterial = tilesToCombine[0].GetComponent <MeshRenderer>().sharedMaterial;

            List <CombineInstance> combine     = new List <CombineInstance>();
            CombineInstance        tempCombine = new CombineInstance();

            int vertexCount = 0;

            foreach (GameObject mesh in tilesToCombine)
            {
                vertexCount += mesh.GetComponent <MeshFilter>().sharedMesh.vertexCount;

                if (vertexCount > 60000)
                {
                    vertexCount = 0;
                    newSubMesh(combine, mesh.GetComponent <MeshRenderer>().sharedMaterial, tileParentObject, destinationPath, customBrushGUID);
                    combine = new List <CombineInstance>();
                }

                if (mesh.GetComponent <MeshRenderer>().sharedMaterial.name != previousMaterial.name)
                {
                    newSubMesh(combine, previousMaterial, tileParentObject, destinationPath, customBrushGUID);
                    combine = new List <CombineInstance>();
                }

                tempCombine.mesh      = mesh.GetComponent <MeshFilter>().sharedMesh;
                tempCombine.transform = mesh.GetComponent <MeshFilter>().transform.localToWorldMatrix;
                combine.Add(tempCombine);
                previousMaterial = mesh.GetComponent <MeshRenderer>().sharedMaterial;
            }

            newSubMesh(combine, previousMaterial, tileParentObject, destinationPath, customBrushGUID);

            tileParentObject.transform.position = Vector3.zero;

            // Add the prefab to the project

#if UNITY_2018_3_OR_NEWER
            PrefabUtility.SaveAsPrefabAsset(tileParentObject, destinationPath + tileParentObject.name);
#else
            PrefabUtility.CreatePrefab(destinationPath + tileParentObject.name, tileParentObject);
#endif
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh(); // refesh the asset database to tell unity changes have been made

            // remove our temporary builder object

            Debug.Log("Custom Brush " + tileParentObject.name + " created.");

            DestroyImmediate(tileParentObject);
            MAP_Editor.selectTiles.Clear();

            // reload the custom brushes
            MAP_Editor.loadCustomBrushes();
        }
    }