修改调整
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
using UnityEngine;
|
||||
using FairyGUI;
|
||||
|
||||
public class EmitComponent : GComponent
|
||||
{
|
||||
GLoader _symbolLoader;
|
||||
GTextField _numberText;
|
||||
Transform _owner;
|
||||
|
||||
const float OFFSET_ADDITION = 2.2f;
|
||||
static Vector2 JITTER_FACTOR = new Vector2(80, 80);
|
||||
|
||||
public EmitComponent()
|
||||
{
|
||||
this.touchable = false;
|
||||
|
||||
_symbolLoader = new GLoader();
|
||||
_symbolLoader.autoSize = true;
|
||||
AddChild(_symbolLoader);
|
||||
|
||||
_numberText = new GTextField();
|
||||
_numberText.autoSize = AutoSizeType.Both;
|
||||
|
||||
AddChild(_numberText);
|
||||
}
|
||||
|
||||
public void SetHurt(Transform owner, int type, long hurt, bool critical)
|
||||
{
|
||||
_owner = owner;
|
||||
|
||||
TextFormat tf = _numberText.textFormat;
|
||||
if (type == 0)
|
||||
tf.font = EmitManager.inst.hurtFont1;
|
||||
else
|
||||
tf.font = EmitManager.inst.hurtFont2;
|
||||
_numberText.textFormat = tf;
|
||||
_numberText.text = "-" + hurt;
|
||||
|
||||
if (critical)
|
||||
_symbolLoader.url = EmitManager.inst.criticalSign;
|
||||
else
|
||||
_symbolLoader.url = "";
|
||||
|
||||
UpdateLayout();
|
||||
|
||||
this.alpha = 1;
|
||||
UpdatePosition(Vector2.zero);
|
||||
Vector2 rnd = Vector2.Scale(UnityEngine.Random.insideUnitCircle, JITTER_FACTOR);
|
||||
int toX = (int)rnd.x * 2;
|
||||
int toY = (int)rnd.y * 2;
|
||||
|
||||
EmitManager.inst.view.AddChild(this);
|
||||
GTween.To(Vector2.zero, new Vector2(toX, toY), 1f).SetTarget(this)
|
||||
.OnUpdate((GTweener tweener) => { this.UpdatePosition(tweener.value.vec2); }).OnComplete(this.OnCompleted);
|
||||
this.TweenFade(0, 0.5f).SetDelay(0.5f);
|
||||
}
|
||||
|
||||
void UpdateLayout()
|
||||
{
|
||||
this.SetSize(_symbolLoader.width + _numberText.width, Mathf.Max(_symbolLoader.height, _numberText.height));
|
||||
_numberText.SetXY(_symbolLoader.width > 0 ? (_symbolLoader.width + 2) : 0,
|
||||
(this.height - _numberText.height) / 2);
|
||||
_symbolLoader.y = (this.height - _symbolLoader.height) / 2;
|
||||
}
|
||||
|
||||
void UpdatePosition(Vector2 pos)
|
||||
{
|
||||
Vector3 ownerPos = _owner.position;
|
||||
ownerPos.y += OFFSET_ADDITION;
|
||||
Vector3 screenPos = Camera.main.WorldToScreenPoint(ownerPos);
|
||||
screenPos.y = Screen.height - screenPos.y; //convert to Stage coordinates system
|
||||
|
||||
Vector3 pt = GRoot.inst.GlobalToLocal(screenPos);
|
||||
this.SetXY(Mathf.RoundToInt(pt.x + pos.x - this.actualWidth / 2), Mathf.RoundToInt(pt.y + pos.y - this.height));
|
||||
}
|
||||
|
||||
void OnCompleted()
|
||||
{
|
||||
_owner = null;
|
||||
EmitManager.inst.view.RemoveChild(this);
|
||||
EmitManager.inst.ReturnComponent(this);
|
||||
}
|
||||
|
||||
public void Cancel()
|
||||
{
|
||||
_owner = null;
|
||||
if (this.parent != null)
|
||||
{
|
||||
GTween.Kill(this);
|
||||
EmitManager.inst.view.RemoveChild(this);
|
||||
}
|
||||
EmitManager.inst.ReturnComponent(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d8535a403e7324c49890b1de70edc2a0
|
||||
timeCreated: 1446570746
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
50
Assets/Plugins/FairyGUI/Examples/EmitNumbers/EmitManager.cs
Normal file
50
Assets/Plugins/FairyGUI/Examples/EmitNumbers/EmitManager.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using FairyGUI;
|
||||
|
||||
public class EmitManager
|
||||
{
|
||||
static EmitManager _instance;
|
||||
public static EmitManager inst
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
_instance = new EmitManager();
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
public string hurtFont1;
|
||||
public string hurtFont2;
|
||||
public string criticalSign;
|
||||
|
||||
public GComponent view { get; private set; }
|
||||
|
||||
private readonly Stack<EmitComponent> _componentPool = new Stack<EmitComponent>();
|
||||
|
||||
public EmitManager()
|
||||
{
|
||||
hurtFont1 = "ui://EmitNumbers/number1";
|
||||
hurtFont2 = "ui://EmitNumbers/number2";
|
||||
criticalSign = "ui://EmitNumbers/critical";
|
||||
|
||||
view = new GComponent();
|
||||
GRoot.inst.AddChild(view);
|
||||
}
|
||||
|
||||
public void Emit(Transform owner, int type, long hurt, bool critical)
|
||||
{
|
||||
EmitComponent ec;
|
||||
if (_componentPool.Count > 0)
|
||||
ec = _componentPool.Pop();
|
||||
else
|
||||
ec = new EmitComponent();
|
||||
ec.SetHurt(owner, type, hurt, critical);
|
||||
}
|
||||
|
||||
public void ReturnComponent(EmitComponent com)
|
||||
{
|
||||
_componentPool.Push(com);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 335d4e8607937704285d9e0aec495020
|
||||
timeCreated: 1446570745
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,44 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using FairyGUI;
|
||||
|
||||
public class EmitNumbersMain : MonoBehaviour
|
||||
{
|
||||
Transform _npc1;
|
||||
Transform _npc2;
|
||||
bool _finished;
|
||||
|
||||
void Start()
|
||||
{
|
||||
Application.targetFrameRate = 60;
|
||||
Stage.inst.onKeyDown.Add(OnKeyDown);
|
||||
|
||||
_npc1 = GameObject.Find("npc1").transform;
|
||||
_npc2 = GameObject.Find("npc2").transform;
|
||||
|
||||
StartCoroutine(RunTest());
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
_finished = true;
|
||||
}
|
||||
|
||||
IEnumerator RunTest()
|
||||
{
|
||||
while (!_finished)
|
||||
{
|
||||
EmitManager.inst.Emit(_npc1, 0, UnityEngine.Random.Range(100, 100000), UnityEngine.Random.Range(0, 10) == 5);
|
||||
EmitManager.inst.Emit(_npc2, 1, UnityEngine.Random.Range(100, 100000), UnityEngine.Random.Range(0, 10) == 5);
|
||||
yield return new WaitForSeconds(0.3f);
|
||||
}
|
||||
}
|
||||
|
||||
void OnKeyDown(EventContext context)
|
||||
{
|
||||
if (context.inputEvent.keyCode == KeyCode.Escape)
|
||||
{
|
||||
Application.Quit();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3ee84d8c960d2404893a09cb4009bee8
|
||||
timeCreated: 1446569244
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user