Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
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 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);
         }
     }
 }
 
Comment
Add comment
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
9
Best Answer

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;
     }
 }
Comment
Add comment · 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.

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

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

Sprite Shape completely incorrect in build. 0 Answers


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