public void DisplayText(string text, GameObject prefab, BattleTextAnimation animation, Color color) { if (!string.IsNullOrEmpty(text)) { if (SingleInstance && instance != null) { GameObject.Destroy(instance); } switch (Source) { case PositionSource.Screen: instance = BattleTextAnimator.SpawnText(text, prefab, animation, ScreenPosition, color); break; case PositionSource.Transform: if (FollowTargetPosition) { instance = BattleTextAnimator.SpawnText(text, prefab, animation, Target, TargetWorldOffset, color); } else { instance = BattleTextAnimator.SpawnText(text, prefab, animation, Target.position + TargetWorldOffset, color); } break; } // Set source specific properties instance.layer = gameObject.layer; if (!SingleInstance) { instance = null; } } }
static GameObject SpawnText( string text, GameObject prefab, BattleTextAnimation animation, Transform target, Vector3 targetWorldOffset, Vector2 screenPosition, Color color) { GameObject instance = (GameObject)GameObject.Instantiate(prefab); BattleTextRenderer renderer = instance.GetComponent <BattleTextRenderer>(); BattleTextAnimator animator = instance.AddComponent <BattleTextAnimator>(); if (!renderer.IsAnimated) { Debug.LogError(string.Format("[BattleTextAnimator] BattleTextRenderer script on '{0}' does not have 'Is Animated' set", prefab.name)); // Disable object instance.SetActiveRecursively(false); // Destroy it GameObject.Destroy(instance); return(null); } // Renderer settings renderer.InitialText = text; renderer.Color = color; renderer.FadeDelay = animation.FadeDelay; renderer.LockXRotation = false; renderer.LookAtMainCamera = animation.AnimateInWorld; // Animator settings animator.positions = animation.ClonePosition(); animator.Target = target; animator.Animation = animation; animator.WorldOffset = targetWorldOffset; animator.ScreenPosition = screenPosition; animator.RandomOffset = new Vector3( UnityEngine.Random.Range(-animation.RandomOffset.x, animation.RandomOffset.x), UnityEngine.Random.Range(-animation.RandomOffset.y, animation.RandomOffset.y), UnityEngine.Random.Range(-animation.RandomOffset.z, animation.RandomOffset.z) ); if (animation.HasRandomPosition) { for (int i = 0; i < animator.positions.Length; ++i) { if (animator.positions[i].Random) { Vector3 a = animator.positions[i].Amount; animator.positions[i].Amount.x = Random.Range(-a.x, a.x); animator.positions[i].Amount.y = Random.Range(-a.y, a.y); animator.positions[i].Amount.z = Random.Range(-a.z, a.z); } } } return(instance); }