Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine.UI;
  4. using UnityEngine;
  5. using Twitter;
  6.  
  7. public class TweetObject : MonoBehaviour
  8. {
  9.  
  10. private TweetFactory tweetFactory = TweetFactory.Instance;
  11. public Tweet self;
  12. public int tweetNumber;
  13. public GameObject tweetProfilePicture;
  14. public GameObject tweetProfilePictureMesh;
  15. public Text tweetText;
  16.  
  17. [SerializeField]
  18. public float lifetime = 60;
  19.  
  20.  
  21. // Use this for initialization so we can set variables from outside the class
  22. public void Init()
  23. {
  24.  
  25.  
  26. //Set the position so that tweets dont spawn on top of each other
  27. transform.position = new Vector3(Mathf.Floor(tweetNumber % 10) / 2, (tweetNumber % 10) / 2, 5f);
  28.  
  29. //Set the text of the tweet object to the text from the tweet data object
  30. tweetText.text = self.text;
  31.  
  32.  
  33. }
  34.  
  35.  
  36. //Our start function is a co-routine here
  37. IEnumerator Start()
  38. {
  39.  
  40. //The using keyword allows us to basically make the construction of the www object a function call
  41. using (WWW www = new WWW(self.user.profile_image_url))
  42. {
  43.  
  44. //Wait for the www object to return with the profile image
  45. yield return www;
  46.  
  47. //Grab a reference to our sprite renderer to display the profile image
  48. SpriteRenderer renderer = tweetProfilePicture.GetComponent<SpriteRenderer>();
  49. //Create a new sprite from the data we grabbed from the www Object
  50. renderer.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0.5f, 0.5f));
  51. //What is going on here, we are setting the texture of the mesh and we are also setting the sprite?
  52. tweetProfilePictureMesh.GetComponent<Renderer>().material.mainTexture = www.texture;
  53.  
  54.  
  55. }
  56. }
  57.  
  58.  
  59. void Update()
  60. {
  61. //Check the OVRGrabbled script attached to see if the tweet is currently grabbed
  62. if (GetComponent<OVRGrabbable>().isGrabbed)
  63. {
  64.  
  65. //Make sure that we re-add the tweet to the cache
  66. //when its interacted with to ensure that its not pushed off the end of the cache
  67.  
  68. //The only problem with this currently is that the tweet will keep added itself to the cache while its grabbed,
  69. //This could be a huge performance hit
  70. tweetFactory.cacheTweet(self);
  71.  
  72. }
  73.  
  74. //The tweets themselves know if they are managed by the cache or not,
  75. //honestly I dont know if this is a good implmenetation or not
  76. //Seems like this would be a better fit in an interface slapped on the
  77. //tweet so that my cache implementation remained generic
  78. if (self.Managed == false || lifetime <= 0f)
  79. {
  80. //Once we fall off the end of the cache destroy ourselves
  81. Destroy(this.gameObject);
  82. }
  83.  
  84.  
  85. //Decrease our lifetime by deltatime
  86. lifetime -= Time.deltaTime;
  87.  
  88.  
  89. }
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement