• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by $$anonymous$$ · Feb 03, 2014 at 11:01 PM · c#2dgameobjecteditorsprite

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 t$$anonymous$$ng 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 t$$anonymous$$s sprite.

Maybe someone knows if t$$anonymous$$s 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 t$$anonymous$$s. 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 t$$anonymous$$s 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.orthograp$$anonymous$$c = 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);
         }
     }
 }
 
Comment
adnan-zaman-13

People who like this

1 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by zee_ola05 · Feb 04, 2014 at 01:36 AM

I didn't read everyt$$anonymous$$ng, but t$$anonymous$$s 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;
     }
 }
Comment
justin35f
Patel-Sagar
ajotatxe
AkooleGame
LilGames
ow3n
adnan-zaman-13
trapstreetgames
EarthHobbit

People who like this

9 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Patel-Sagar · Jul 17, 2014 at 07:08 AM 0
Share

worked for me....nice solution.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Set Light2D sprite via scripting 1 Answer

Strange artifacts in builds 0 Answers

Sprite Swapping help 1 Answer

enable and disable loop objects in the scene 1 Answer

How to break a sprite into shapes with script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges