private void DoLateOnValidate() { EditorApplication.update -= DoLateOnValidate; if (!this || !ParentTilemap) { return; } //Debug.Log("DoLateOnValidate " + ParentTilemap.name + "/" + name, gameObject); // fix prefab preview if (EditorCompatibilityUtils.IsPrefab(gameObject) && !gameObject.scene.IsValid()) { #if !UNITY_2018_3_OR_NEWER // disabled prefab preview for performance issue with new prefab workflow m_needsRebuildMesh = true; UpdateMesh(true); #endif } else { m_needsRebuildMesh = true; m_needsRebuildColliders = true; if (ParentTilemap) //NOTE: this is null sometimes in Unity 2017.2.0b4. It happens when the brush is changed, so maybe it's related with the brush but transform.parent is null. { ParentTilemap.UpdateMesh(); } } }
private void DoLateOnValidate() { EditorApplication.update -= DoLateOnValidate; if (!this || !ParentTilemap) { return; } //Debug.Log("DoLateOnValidate " + ParentTilemap.name + "/" + name, gameObject); // fix prefab preview if (EditorCompatibilityUtils.IsPrefab(gameObject)) { m_needsRebuildMesh = true; UpdateMesh(); } else { m_needsRebuildMesh = true; m_needsRebuildColliders = true; if (ParentTilemap) //NOTE: this is null sometimes in Unity 2017.2.0b4. It happens when the brush is changed, so maybe it's related with the brush but transform.parent is null. { ParentTilemap.UpdateMesh(); } } }
private GameObject CreateTileObject(int tileIdx, TilePrefabData tilePrefabData) { if (tilePrefabData.prefab != null) { TileObjData tileObjData = FindTileObjDataByTileIdx(tileIdx); GameObject tileObj = null; int gx = tileIdx % m_width; int gy = tileIdx / m_width; if (tileObjData == null || tileObjData.tilePrefabData != tilePrefabData || tileObjData.obj == null) { #if UNITY_EDITOR GameObject sourcePrefabInstance = null; if (BrushBehaviour.TileObjSourceTilemap) { // Use the brush instance to copy also the modified parameters Vector2Int copyLoc = new Vector2Int(GridPosX + gx + BrushBehaviour.TileObjSourceTilemapOffset.x, GridPosY + gy + BrushBehaviour.TileObjSourceTilemapOffset.y); sourcePrefabInstance = BrushBehaviour.TileObjSourceTilemap.GetTileObject(copyLoc.x, copyLoc.y); } if (sourcePrefabInstance && !EditorCompatibilityUtils.IsPrefab(sourcePrefabInstance)) { tileObj = (GameObject)Instantiate(sourcePrefabInstance, Vector3.zero, transform.rotation); tileObj.name = tilePrefabData.prefab.name; } else { tileObj = (GameObject)UnityEditor.PrefabUtility.InstantiatePrefab(tilePrefabData.prefab); if (sourcePrefabInstance) { UnityEditor.PropertyModification[] modifications = UnityEditor.PrefabUtility.GetPropertyModifications(sourcePrefabInstance); UnityEditor.PrefabUtility.SetPropertyModifications(tileObj, modifications); } } // allow destroy the object with undo operations if (ParentTilemap.IsUndoEnabled) { UnityEditor.Undo.RegisterCreatedObjectUndo(tileObj, STETilemap.k_UndoOpName + ParentTilemap.name); } #else tileObj = (GameObject)Instantiate(tilePrefabData.prefab, Vector3.zero, transform.rotation); #endif _SetTileObjTransform(tileObj, gx, gy, tilePrefabData, m_tileDataList[tileIdx]); if (tileObjData != null) { m_tileObjToBeRemoved.Add(tileObjData.obj); tileObjData.obj = tileObj; tileObjData.tilePrefabData = tilePrefabData; } else { m_tileObjList.Add(new TileObjData() { tilePos = tileIdx, obj = tileObj, tilePrefabData = tilePrefabData }); } tileObj.SendMessage(k_OnTilePrefabCreation, new OnTilePrefabCreationData() { ParentTilemap = ParentTilemap, GridX = GridPosX + gx, GridY = GridPosY + gy }, SendMessageOptions.DontRequireReceiver); return(tileObj); } else if (tileObjData.obj != null) { #if UNITY_EDITOR && !UNITY_2018_3_OR_NEWER //+++ Break tilemap prefab and restore tile prefab link GameObject parentPrefab = UnityEditor.PrefabUtility.FindRootGameObjectWithSameParentPrefab(tileObjData.obj); if (parentPrefab != tileObjData.obj) { DestroyImmediate(tileObjData.obj); tileObjData.obj = UnityEditor.PrefabUtility.InstantiatePrefab(tileObjData.tilePrefabData.prefab) as GameObject; } ///--- #endif _SetTileObjTransform(tileObjData.obj, gx, gy, tilePrefabData, m_tileDataList[tileIdx]); tileObjData.obj.SendMessage(k_OnTilePrefabCreation, new OnTilePrefabCreationData() { ParentTilemap = ParentTilemap, GridX = GridPosX + gx, GridY = GridPosY + gy }, SendMessageOptions.DontRequireReceiver); return(tileObjData.obj); } } return(null); }
/// <summary> /// Update the mesh and return false if all tiles are empty /// </summary> /// <returns></returns> public bool UpdateMesh() { if (ParentTilemap == null) { if (transform.parent == null) { gameObject.hideFlags = HideFlags.None; //Unhide orphan tilechunks. This shouldn't happen } ParentTilemap = transform.parent.GetComponent <STETilemap>(); } if (gameObject.layer != ParentTilemap.gameObject.layer) { gameObject.layer = ParentTilemap.gameObject.layer; } if (!gameObject.CompareTag(ParentTilemap.gameObject.tag)) { gameObject.tag = ParentTilemap.gameObject.tag; } transform.localPosition = new Vector2(GridPosX * CellSize.x, GridPosY * CellSize.y); if (m_meshFilter.sharedMesh == null) { //Debug.Log("Creating new mesh for " + name); m_meshFilter.sharedMesh = new Mesh(); m_meshFilter.sharedMesh.hideFlags = HideFlags.DontSave; m_meshFilter.sharedMesh.name = ParentTilemap.name + "_mesh"; m_needsRebuildMesh = true; } #if UNITY_EDITOR // fix prefab preview, not compatible with MaterialPropertyBlock. I need to create a new material and change the main texture and color directly. if (EditorCompatibilityUtils.IsPrefab(gameObject)) { gameObject.hideFlags |= HideFlags.HideInHierarchy; if (m_meshRenderer.sharedMaterial == null || m_meshRenderer.sharedMaterial == ParentTilemap.Material) { m_meshRenderer.sharedMaterial = new Material(ParentTilemap.Material); m_meshRenderer.sharedMaterial.name += "_copy"; m_meshRenderer.sharedMaterial.hideFlags = HideFlags.DontSave; m_meshRenderer.sharedMaterial.color = ParentTilemap.TintColor; m_meshRenderer.sharedMaterial.mainTexture = ParentTilemap.Tileset ? ParentTilemap.Tileset.AtlasTexture : null; } } else #endif //NOTE: else above { m_meshRenderer.sharedMaterial = ParentTilemap.Material; } m_meshRenderer.enabled = ParentTilemap.IsVisible; if (m_needsRebuildMesh) { m_needsRebuildMesh = false; if (FillMeshData()) { m_invalidateBrushes = false; Mesh mesh = m_meshFilter.sharedMesh; mesh.Clear(); UpdateSortOrder(); #if UNITY_5_0 || UNITY_5_1 mesh.vertices = s_vertices.ToArray(); mesh.triangles = s_triangles.ToArray(); mesh.uv = m_uv.ToArray(); if (s_colors32 != null && s_colors32.Count != 0) { mesh.colors32 = s_colors32.ToArray(); } else { mesh.colors32 = null; } #else mesh.SetVertices(s_vertices); mesh.SetTriangles(s_triangles, 0); mesh.SetUVs(0, m_uv); if (s_colors32 != null && s_colors32.Count != 0) { mesh.SetColors(s_colors32); } else { mesh.SetColors((List <Color32>)null); } #endif mesh.RecalculateNormals(); //NOTE: allow directional lights to work properly TangentSolver(mesh); //NOTE: allow bumped shaders to work with directional lights } else { return(false); } } else if (m_needsRebuildMeshColor) { UpdateMeshVertexColor(); Mesh mesh = m_meshFilter.sharedMesh; #if UNITY_5_0 || UNITY_5_1 if (s_colors32 != null && s_colors32.Count != 0) { mesh.colors32 = s_colors32.ToArray(); } else { mesh.colors32 = null; } #else if (s_colors32 != null && s_colors32.Count != 0) { mesh.SetColors(s_colors32); } else { mesh.SetColors((List <Color32>)null); } #endif } m_needsRebuildMeshColor = false; return(true); }