- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
- 纳金币
- -1
- 精华
- 11
|
Step 3
Place these two scripts in the Editor folder:
/*
Copyright 2010 CJ Currie
@Date: October 2010
@Contact:
CJCurrie@BlackStormsStudios.com
@Script: A custom Inspector and popout window for choosing tiles from a tileset
@Connections:
@TODO: Suppress the error thrown on line 58.
*/
using UnityEngine;
using UnityEditor;
[CustomEditor (typeof(TileManager))]
public class TileManagerEditor : Editor {
public static Texture2D tileset;
int tileSize = 16;
TileManager targ;
//public Texture2D currentTile;
public override void OnInspectorGUI()
{
targ = (TileManager)target;
tileset = (Texture2D)EditorGUILayout.ObjectField("Current tileset", tileset, typeof(Texture2D));
GUILayout.BeginHorizontal();
GUILayout.Label("Unique ID: ");
targ.uniqueID = EditorGUILayout.TextField (targ.uniqueID, GUILayout.Width(100));
GUILayout.Label(" Tile Size ");
tileSize = EditorGUILayout.IntField(tileSize, GUILayout.Width(20));
GUILayout.EndHorizontal();
if (GUILayout.Button("Select new tile"))
{
TileSelectorEditor window = (TileSelectorEditor) EditorWindow.GetWindow (typeof (TileSelectorEditor));
window.Init(this, tileSize, tileset);
}
GUILayout.Space(30);
GUILayout.Label("Last tile configuration:
"+"("+targ.lastTile[0]+","+targ.lastTile[1]+") to ("+
(targ.lastTile[0]+targ.lastTile[2])+","+(targ.lastTile[1]+targ.lastTile[3])+")");
if (GUILayout.Button ("Reselect last tile configuration"))
{
SetTile(targ.lastTile[0], targ.lastTile[1], targ.lastTile[2], targ.lastTile[3]);
//((TileManager)target).renderer.material.mainTexture = ((TileManager)target).currentTile;
}
GUILayout.BeginHorizontal();
GUILayout.Label ("Last selected tile: ");
GUILayout.Label (targ.currentTile);
GUILayout.EndHorizontal();
GUILayout.Space(20);
if (GUILayout.Button("RESET: Lose all assets and selection data?"))
{
targ.lastTile = new int[4];
AssetDatabase.DeleteAsset(targ.uniquePath + "
_
tex.asset");
AssetDatabase.DeleteAsset(targ.uniquePath + "
_
mat.asset");
targ.uniquePath = "";
targ.uniqueID = "";
}
}
// =================
// Remotely Called
// =================
public void SetTile (int x, int y, int width, int height)
{
if (!tileset)
{
Debug.LogError ("No Tileset selected");
return;
}
if (width == 0 || height == 0)
{
Debug.LogError ("Cannot select a tile of width or height 0");
return;
}
// Set the last tile so it can be re-fetched
targ.lastTile[0] = x;
targ.lastTile[1] = y;
targ.lastTile[2] = width;
targ.lastTile[3] = height;
// Set other info on the TileManager
targ.width = width;
targ.height = height;
// Pick the tile's texture
int tHeight = tileset.height;
y += - 2 + height; // Do a little offsetting because the way the pixels are mapped is weird
y = (tHeight/tileSize) - y; // Invert since GetPixels is bottom-based
// --- Make the new texture
Color[] pixels = tileset.GetPixels (x*tileSize, y*tileSize, width*tileSize, height*tileSize);
Texture2D newTex = new Texture2D (width*tileSize, height*tileSize);
newTex.SetPixels(pixels);
newTex.mipMapBias = -10;
newTex.Apply(false);
// --- Modify the assets we've created ---
targ.uniquePath = "Assets/Map Pieces/Components/Images/" + targ.uniqueID;
AssetDatabase.CreateAsset(newTex, targ.uniquePath+"
_
tex.asset");
targ.currentTile = newTex; // Set the icon
// --- Make the new Material
Material newMat = new Material (targ.renderer.sharedMaterial);
// --- Apply texture to the material - Note that on slower machines it may attempt to do this before the asset is finished creating, and a nullreference will be thrown. Ignore it
//
newMat.mainTexture = (Texture2D)AssetDatabase.LoadAssetAtPath (targ.uniquePath+"
_
tex.asset", typeof(Texture2D) );
AssetDatabase.CreateAsset (newMat, targ.uniquePath+"
_
mat.asset");
try
{
targ.renderer.sharedMaterial = (Material)AssetDatabase.LoadAssetAtPath (targ.uniquePath+"
_
mat.asset", typeof(Material) );
//targ.renderer.sharedMaterial.mainTexture = (Texture2D)AssetDatabase.LoadAssetAtPath (targ.uniquePath, typeof(Texture2D) );
}
catch (System.Exception e)
{
Debug.LogError (e);
}
}
}
Note that the path on line 108 needs to be modified from "Assets/Map Pieces/Components/Images/" to "Assets/your path".
This is the other:
/*
Copyright 2010 CJ Currie
@Date: October 2010
@Contact:
CJCurrie@BlackStormsStudios.com
@Script: Creates the window that chooses the tiles and submits them to the TileManagerEditor.
@Connections:
@TODO:
*/
using UnityEngine;
using UnityEditor;
public class TileSelectorEditor : EditorWindow {
// --- Cache ---
public Texture2D selectionBox;
// --- Init Values ---
TileManagerEditor target;
Vector2 scrollview;
// --- Script vars ---
Texture2D tileset = null;
int tileSize;
Vector2 mousePos;
float zoom = 1.5f;
//int[] numberOfTiles = new int[2];
int[] selectedStart = new int[2]; // The top-left-most tile selected
int horizTilesSelected; // How many tiles wide the selection is
int vertTilesSelected; // How many tiles tall
bool isDragging;
int padding
_
top = 2;
public void Init(TileManagerEditor incTarget, int incTileSize, Texture2D inctileset)
{
wantsMouseMove = ***e;
target = (TileManagerEditor)incTarget;
tileSize = incTileSize;
//numberOfTiles[0] = tileset.width / tileSize;
//numberOfTiles[1] = tileset.height / tileSize;
tileset = inctileset;
}
void OnGUI()
{
if (!tileset)
{
GUILayout.FlexibleSpace();
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.Label("NO TILESET SELECTED");
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.FlexibleSpace();
return;
}
// Zoom controls
GUILayout.BeginHorizontal(GUILayout.Width(300), GUILayout.Height(tileSize*padding
_
top*zoom));
GUILayout.Label (" Zoom ");
// Check that we're not zooming too far, too
if (GUILayout.Button(" + ") && zoom < 4)
{zoom += .5f;}
else if (GUILayout.Button(" - ") && zoom > 1)
{zoom -= .5f;}
GUILayout.Space(50);
// Submit changes
if (GUILayout.Button("Set Sprite", GUILayout.Width(200)))
{
// Increment the width and height so it's no longer zero-based
target.SetTile(selectedStart[0], selectedStart[1], horizTilesSelected + 1, vertTilesSelected + 1);
}
GUILayout.Label("Selected: ("+selectedStart[0]+","+selectedStart[1]+") to ("+(selectedStart[0]+horizTilesSelected)+","+(selectedStart[1]+vertTilesSelected)+")" );
GUILayout.EndHorizontal();
// -----------------
// Begin scroll view
// Make the scroll bar
scrollview = EditorGUILayout.BeginScrollView(scrollview, GUILayout.Width (position.width), GUILayout.Height (position.height));
// Round to the nearest tileSize. Dividing and then multiplying by the same number forces scrollview to be a multiple of tileSize
scrollview.x = (int)(scrollview.x/tileSize/zoom);
scrollview.x *= tileSize*zoom;
scrollview.y = (int)(scrollview.y/tileSize/zoom);
scrollview.y *= tileSize*zoom;
// Show the tileset
GUILayout.Label("", GUILayout.Width(tileset.width * zoom), GUILayout.Height(tileset.height * zoom));
GUI.DrawTexture(new Rect(0,0,tileset.width * zoom,tileset.height * zoom), tileset);
// End scroll view
EditorGUILayout.EndScrollView ();
// ------------------
Event e = Event.current;
int[] currentPos = new int[2];
currentPos[0] = (int)((e.mousePosition.x + scrollview.x) / tileSize / zoom);
currentPos[1] = (int)((e.mousePosition.y + scrollview.y) / tileSize / zoom);
// --- Tile select ---
if (isDragging)
{
// We're back on the first tile
if (currentPos[0] == selectedStart[0])
horizTilesSelected = 0;
if (currentPos[1] == selectedStart[1])
vertTilesSelected = 0;
if (currentPos != selectedStart)
{
// Are we to the left of the selectedStart?
if (currentPos[0] < selectedStart[0])
{
horizTilesSelected ++; // Change how many tiles wide the selection is
selectedStart[0] = currentPos[0]; // Change the selected start
}
// Else, if we are to the right, change width
else if (selectedStart[0] < currentPos[0])
horizTilesSelected = currentPos[0] - selectedStart[0]; // Change the width
// Are we above the selectedStart?
if (currentPos[1] < selectedStart[1])
{
vertTilesSelected = selectedStart[1] - currentPos[1]; // Change how many tiles tall the selection is
selectedStart[1] = currentPos[1]; // Change the selected start
}
// Else, if we are below, change height
else if (selectedStart[1] < currentPos[1])
vertTilesSelected = currentPos[1] - selectedStart[1];
}
}
if (e.type == EventType.MouseDown && e.button == 0) // Left mouse button down - first selection
{
selectedStart[0] = currentPos[0];
selectedStart[1] = currentPos[1];
horizTilesSelected = 0;
vertTilesSelected = 0;
isDragging = ***e;
}
else if (isDragging && e.type == EventType.MouseUp && e.button == 0)
{
isDragging = false;
}
int[] offset = new int[2];
offset[0] = (int)(scrollview.x / tileSize / zoom);
offset[1] = (int)(scrollview.y / tileSize / zoom);
// Selection Indicator
GUI.DrawTexture(new Rect((selectedStart[0]-offset[0]) * zoom * tileSize, (selectedStart[1]-offset[1]) * zoom * tileSize, tileSize*zoom * (horizTilesSelected+1),
tileSize*zoom * (vertTilesSelected+1)), selectionBox);
// --- If we're in the window, redraw it
if (EditorWindow.mouseOverWindow == this)
{
this.Repaint();
}
}
}
Once the script compiles, set the default "Selection Box" reference image by clicking on the script and dragging the image to the exposed variable. I used a simple one I made in photoshop:
|
|