- Home /
Assigning a Sprite to a Script and creating a Gameobject out of that sprite
Hey, I am creating some Scripts to improve the work with unity's 2D-Tools. So I created a Script that changes the Scene-view to a tile-Based editor with snap-grid. And now i have a little thing I want to improve. Right now I create a Game-Object when leftclicking but in the inspector I have to assign a prefab of a gameobject with a sprite renderer on it to tell the editor what object should be created. And I dont want to create a prefab for every Sprite that is in the game. And now i would like to know if there is a way to convert a sprite into an gameobject, because i just want to be able to assign a Sprite in the inspector and afterwards when i left click its creates the gameobject out of this sprite.
Maybe someone knows if this works and can tell me how. If it isnt working that way I am happy if you maybe tell me some other ways to do this. Even if you have a completley different attempt that would work I want to know.
Here my scripts(If you don't know what a special line of the code does just ask):
using UnityEngine;
using System.Collections;
public class LevelDesigner : MonoBehaviour {
public GameObject prefab; // I want this to be a Sprite and not a gameobject
public Vector2 gizmoPosition;
public float depth =0;
void OnDrawGizmos(){
Gizmos.DrawWireCube(new Vector3(gizmoPosition.x,gizmoPosition.y,depth), new Vector3(1,1,1));
}
}
And the 2nd scripr:
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(LevelDesigner))]
public class LevelDesignerEditor : Editor
{
LevelDesigner script;
Vector2 oldTilePos = new Vector2 ();
void OnEnable ()
{
script = (LevelDesigner)target;
if(!Application.isPlaying){
if(SceneView.lastActiveSceneView != null){
SceneView.lastActiveSceneView.orthographic = true;
SceneView.lastActiveSceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot,Quaternion.identity);
}
}
}
public override void OnInspectorGUI ()
{
script.depth = EditorGUILayout.Slider (script.depth, -5, 5);
EditorGUILayout.BeginHorizontal ();
EditorGUILayout.PrefixLabel ("Tile(prefab)");
script.prefab = (GameObject)EditorGUILayout.ObjectField (script.prefab, typeof(GameObject), false);
EditorGUILayout.EndHorizontal();
if (GUI.changed)
EditorUtility.SetDirty (target);
}
void OnSceneGUI ()
{
Ray ray = HandleUtility.GUIPointToWorldRay (Event.current.mousePosition);
Vector2 tilePos = new Vector2 ();
tilePos.x = Mathf.RoundToInt (ray.origin.x);
tilePos.y = Mathf.RoundToInt (ray.origin.y);
if (tilePos != oldTilePos) {
script.gizmoPosition = tilePos;
SceneView.RepaintAll ();
oldTilePos = tilePos;
}
Event current = Event.current;
if(current.type == EventType.mouseDown)
{
string name = string.Format("Tile{0}_{1}_{2}",script.depth,tilePos.y,tilePos.x);
if(current.button ==0)
{
CreateTile(tilePos,name);
}
if(current.button ==1)
{
DeleteTile(name);
}
}
if (GUI.changed)
EditorUtility.SetDirty (target);
}
void CreateTile(Vector2 tilePos, string name)
{
if(!GameObject.Find(name))
{
Vector3 pos = new Vector3(tilePos.x, tilePos.y,script.depth);
GameObject go = (GameObject)Instantiate (script.prefab,pos,Quaternion.identity);
go.name = name;
}
}
void DeleteTile(string name)
{
GameObject go = GameObject.Find(name);
if(go!=null)
{
DestroyImmediate(go);
}
}
}
Answer by zee_ola05 · Feb 04, 2014 at 01:36 AM
I didn't read everything, but this is how I would instantiate a GameObject sprite.
public class Test : MonoBehaviour
{
public Sprite sprite;
void Start()
{
GameObject go = new GameObject("Test");
SpriteRenderer renderer = go.AddComponent<SpriteRenderer>();
renderer.sprite = sprite;
}
}
Your answer
Follow this Question
Related Questions
How to achieve this ? ( pivoting) ! (2D sprite) 0 Answers
Multiple Cars not working 1 Answer
Find point of top of rotated sprite 1 Answer
Itween gradual jump 2d 1 Answer