void LateUpdate() { if( _TileSet != _CachedTileSet ) { _Changed = true; } if( _TileSet != null ) { if( _TileSet.material != _CachedMaterial ) { _Changed = true; } else if( _TileSet.material != null && _TileSet.material.mainTexture != _CachedTexture ) { _Changed = true; } } _CachedTileSet = _TileSet; if( _TileSet != null ) { if( _TileSet.material != _CachedMaterial ) { _CachedMaterial = _TileSet.material; if( _Material != null ) { Object.Destroy( _Material ); _Material = null; } if( _CachedMaterial != null ) { _Material = new Material( _CachedMaterial ); _Material.hideFlags = HideFlags.HideAndDontSave; } } if( _TileSet.material != null ) { _CachedTexture = _TileSet.material.mainTexture; _Material.mainTexture = _TileSet.material.mainTexture; } else { _CachedTexture = null; } } else { _CachedMaterial = null; if( _Material != null ) { Object.Destroy( _Material ); _Material = null; } _CachedTexture = null; } if( _Material != null && _Material.color != _Color ) { _Material.color = _Color; } foreach( Chunk chunk in _Chunks ) { if( _Changed || chunk != null && chunk.dirty ) { chunk.UpdateMesh(); chunk.dirty = false; } } _Changed = false; }
public static int SelectTileField( TileSet tileSet,int value ) { GUILayout.BeginVertical( GUI.skin.box,GUILayout.MinHeight( 32f ) ); if( tileSet != null && tileSet.tiles != null && tileSet.tiles.Length != 0 ) { int tileWidth = (Screen.width - 20) / 32; int tileHeight = Mathf.CeilToInt( (float)tileSet.tiles.Length / (float)tileWidth ); Rect aspectRect = GUILayoutUtility.GetAspectRect( (float)tileWidth / (float)tileHeight ); value = SelectTileField( aspectRect,tileSet,value ); } else { GUILayout.Label( "No tiles defined." ); } GUILayout.EndVertical(); return value; }
public static void DrawTileSetPreview( Rect r,TileSet tileSet,GUIStyle background,bool select,int selectedIndex ) { if (Event.current.type == EventType.Repaint) { background.Draw(r, false, false, false, false); } Texture2D texture = tileSet.material.mainTexture as Texture2D; float scale = Mathf.Min(Mathf.Min(r.width / (float) texture.width, r.height / (float) texture.height), 1f); float width = texture.width * scale; float height = texture.height * scale; Rect rect = new Rect(r.x + (r.width-width)*0.5f, r.y + (r.height-height)*0.5f, width,height ); if( texture != null && texture.alphaIsTransparency ) EditorGUI.DrawTextureTransparent(rect, texture); else EditorGUI.DrawPreviewTexture(rect, texture); if( tileSet.tiles != null && tileSet.tiles.Length>0 ) { handleWireMaterial.SetPass(0); GL.PushMatrix(); GL.MultMatrix(Handles.matrix); GL.Begin(1); for( int i=0;i<tileSet.tiles.Length;++i ) { Tile tile = tileSet.tiles[i]; if( tile == null ) continue; Color color = (select && i==selectedIndex)?Color.cyan:new Color(1.0f,1.0f,1.0f,0.5f); GL.Color( color ); Rect tileRect = new Rect(); switch( tile.type ) { case Tile.Type.Normal: tileRect = new Rect( tile.position.x,tile.position.y, (float)(tile.size * tile.width),(float)(tile.size * tile.height) ); break; case Tile.Type.AutoFloorVX: tileRect = new Rect( tile.position.x,tile.position.y, (float)tile.size * 2.0f,(float)tile.size * 3.0f ); break; case Tile.Type.AutoFloorWolf: tileRect = new Rect( tile.position.x,tile.position.y, (float)tile.size,(float)tile.size * 5.0f); break; } DrawRect( new Rect(){ xMin = rect.xMin + (tileRect.xMin / (float) texture.width) * rect.width, xMax = rect.xMin + (tileRect.xMax / (float) texture.width) * rect.width, yMin = rect.yMin + (tileRect.yMin / (float) texture.height) * rect.height, yMax = rect.yMin + (tileRect.yMax / (float) texture.height) * rect.height } ); } GL.End(); GL.PopMatrix(); } }
public static int SelectTileField( Rect position,TileSet tileSet,int value ) { if( tileSet==null ) { return value; } Texture texture = tileSet.material.mainTexture; float textureWidth = (float)texture.width; float textureHeight = (float)texture.height; int tileNum = tileSet.tiles.Length; Event current = Event.current; GUI.BeginGroup( position ); Rect tilePos = new Rect( 0.0f,0.0f,32,32 ); for( int tileIndex=0;tileIndex<tileNum;tileIndex++ ) { Tile tile = tileSet.tiles[tileIndex]; if( tile == null ) continue; GUI.BeginGroup( tilePos ); if( GUI.Button( new Rect( 0,0,32,32), GUIContent.none,GUI.skin.box) ) { value = tileIndex; } if( current.type == EventType.Repaint ) { Rect texCoord = new Rect( (float)tile.position.x/textureWidth,1.0f-(tile.position.y+tile.size)/textureHeight,tile.size/textureWidth,tile.size/textureHeight ); GUI.DrawTextureWithTexCoords( new Rect(0,0,32,32) ,texture,texCoord,true ); if( value == tileIndex ) { DrawOutline( new Rect(0,0,32,32),Color.cyan ); } } tilePos.x += 32.0f; if( tilePos.x > position.width - 32.0f ) { tilePos.x = 0.0f; tilePos.y += 32.0f; } GUI.EndGroup(); } GUI.EndGroup(); return value; }
public static Vector2 CalcSize( TileSet tileSet ) { int tileNum = tileSet.tiles.Length; int tileHeight = Mathf.CeilToInt( (float)tileNum / (float)_TileWidth ); return new Vector2( _TileWidth*32+4,tileHeight*32+4); }