Advertisement
Guest User

CreateStaticGraphicsObject()

a guest
May 4th, 2015
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1. private void CreateStaticGraphicsObject(GameObject container, JSONNode graphicsObjectJSON)
  2. {
  3.     string name, path;
  4.     int index;
  5.     float x, y, w, h, r;
  6.  
  7.     // Get the data
  8.     name = graphicsObjectJSON["data"]["name"];
  9.     x = graphicsObjectJSON["x"].AsFloat;
  10.     y = graphicsObjectJSON["y"].AsFloat;
  11.     w = graphicsObjectJSON["w"].AsFloat;
  12.     h = graphicsObjectJSON["h"].AsFloat;
  13.     r = graphicsObjectJSON["r"].AsFloat;
  14.     path = graphicsObjectJSON["data"]["graphics path"].Value;
  15.     index = graphicsObjectJSON["data"]["graphics index"].AsInt;
  16.  
  17.     // Create the static graphics object
  18.     GameObject graphicsObject = InstantiatePrefab(_prefabManager.StaticGraphicsObject);
  19.  
  20.     // Set its parent
  21.     graphicsObject.transform.parent = container.transform;
  22.  
  23.     // Set its position, scale and rotation
  24.     graphicsObject.transform.localScale = new Vector3(w, h, 1);
  25.     graphicsObject.transform.localPosition = new Vector3(x, y, 0);
  26.     graphicsObject.transform.rotation = Quaternion.Euler(0, 0, r);
  27.  
  28.     // Give it a name
  29.     graphicsObject.name = name;
  30.  
  31.     // Get a reference to the static graphics object
  32.     StaticGraphicsObject staticGraphicsObject = graphicsObject.GetComponent<StaticGraphicsObject>();
  33.  
  34.     // Set the sprite
  35.     staticGraphicsObject.GraphicsPath = path;
  36.     staticGraphicsObject.GraphicsIndex = index;
  37.  
  38.     // Update the graphics
  39.     staticGraphicsObject.UpdateGraphic();
  40. }
  41.  
  42. private GameObject InstantiatePrefab(UnityEngine.Object prefab)
  43. {
  44.     if (Application.isEditor)
  45.     {
  46.         return (GameObject)UnityEditor.PrefabUtility.InstantiatePrefab(prefab);
  47.     }
  48.     else
  49.         return (GameObject)Instantiate(prefab);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement