Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.UI;
- using UnityEngine;
- using Twitter;
- public class TweetObject : MonoBehaviour
- {
- private TweetFactory tweetFactory = TweetFactory.Instance;
- public Tweet self;
- public int tweetNumber;
- public GameObject tweetProfilePicture;
- public GameObject tweetProfilePictureMesh;
- public Text tweetText;
- [SerializeField]
- public float lifetime = 60;
- // Use this for initialization so we can set variables from outside the class
- public void Init()
- {
- //Set the position so that tweets dont spawn on top of each other
- transform.position = new Vector3(Mathf.Floor(tweetNumber % 10) / 2, (tweetNumber % 10) / 2, 5f);
- //Set the text of the tweet object to the text from the tweet data object
- tweetText.text = self.text;
- }
- //Our start function is a co-routine here
- IEnumerator Start()
- {
- //The using keyword allows us to basically make the construction of the www object a function call
- using (WWW www = new WWW(self.user.profile_image_url))
- {
- //Wait for the www object to return with the profile image
- yield return www;
- //Grab a reference to our sprite renderer to display the profile image
- SpriteRenderer renderer = tweetProfilePicture.GetComponent<SpriteRenderer>();
- //Create a new sprite from the data we grabbed from the www Object
- renderer.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0.5f, 0.5f));
- //What is going on here, we are setting the texture of the mesh and we are also setting the sprite?
- tweetProfilePictureMesh.GetComponent<Renderer>().material.mainTexture = www.texture;
- }
- }
- void Update()
- {
- //Check the OVRGrabbled script attached to see if the tweet is currently grabbed
- if (GetComponent<OVRGrabbable>().isGrabbed)
- {
- //Make sure that we re-add the tweet to the cache
- //when its interacted with to ensure that its not pushed off the end of the cache
- //The only problem with this currently is that the tweet will keep added itself to the cache while its grabbed,
- //This could be a huge performance hit
- tweetFactory.cacheTweet(self);
- }
- //The tweets themselves know if they are managed by the cache or not,
- //honestly I dont know if this is a good implmenetation or not
- //Seems like this would be a better fit in an interface slapped on the
- //tweet so that my cache implementation remained generic
- if (self.Managed == false || lifetime <= 0f)
- {
- //Once we fall off the end of the cache destroy ourselves
- Destroy(this.gameObject);
- }
- //Decrease our lifetime by deltatime
- lifetime -= Time.deltaTime;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement