Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Copyright (c) 2016-2020 Kai Clavier [kaiclavier.com] Do Not Distribute
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- /*
- Example script for spawning a gameObject at a position on a Canvas.
- */
- [RequireComponent(typeof(SuperTextMesh))]
- public class STMCustomUIEvent : MonoBehaviour
- {
- public SuperTextMesh superTextMesh;
- public GameObject spawnThisPrefab;
- public string customTag = "spawn";
- private List<GameObject> spawnedPrefabs = new List<GameObject>();
- //set up STM initially
- public void Reset()
- {
- superTextMesh = GetComponent<SuperTextMesh>();
- }
- //assign custom event
- public void OnEnable()
- {
- superTextMesh.OnCustomEvent += CustomEvent;
- superTextMesh.OnRebuildEvent += ClearAllObjects;
- }
- //unassign
- public void OnDisable()
- {
- superTextMesh.OnCustomEvent -= CustomEvent;
- superTextMesh.OnRebuildEvent -= ClearAllObjects;
- }
- public void CustomEvent(string text, STMTextInfo info)
- {
- if(text == customTag)
- {
- SpawnObject(text, info);
- }
- }
- private GameObject cachedNewObject;
- public void SpawnObject(string text, STMTextInfo info)
- {
- //spawn a copy of the gameobject
- cachedNewObject = Instantiate(spawnThisPrefab);
- //set parent
- cachedNewObject.transform.SetParent(superTextMesh.t);
- spawnedPrefabs.Add(cachedNewObject);
- //set position, rotation, and scale to be relative to text.
- cachedNewObject.transform.position = superTextMesh.t.TransformPoint(info.Middle); //make relative to transform
- cachedNewObject.transform.localRotation = spawnThisPrefab.transform.localRotation; //copy from prefab
- cachedNewObject.transform.localScale = spawnThisPrefab.transform.localScale;
- }
- public void ClearAllObjects()
- {
- foreach(GameObject obj in spawnedPrefabs)
- {
- Destroy(obj);
- }
- spawnedPrefabs.Clear();
- }
- }
Add Comment
Please, Sign In to add comment