public void Initialize(Unit_AI enemy, UnityAction <int> deregisterOnDestroyAction) { if (isInitialized == true) { Debug.Log("This component was already initialized"); return; } this.enemy = enemy; this.deregisterOnDestroyAction = deregisterOnDestroyAction; enemyInstanceID = enemy.GetInstanceID(); mainCamera = GameManager.MainCamera; enemyTransform = enemy.BodyTransform.Head; rt = GetComponent <RectTransform>(); rt.sizeDelta = new Vector2(202, 18); enemy.Status.HP.OnCurrentHealthUpdated.AddListener(SetFillRate); frame = UI_Pool.Instance.GetImage(transform, 202, 18, nameof(frame)); frame.rectTransform.sizeDelta = rt.sizeDelta; frame.rectTransform.localPosition = Vector3.zero; frame.sprite = UI_Battle.Instance.TargetHP.HPST_Sprite; frame.type = Image.Type.Sliced; frame.enabled = true; fill = UI_Pool.Instance.GetImage(transform, (int)width, 16, nameof(fill)); fill.color = Color.red; fill.sprite = UI_Battle.Instance.TargetHP.HPST_Sprite; fill.type = Image.Type.Sliced; fill.rectTransform.pivot = new Vector2(0.0f, 0.5f); fill.rectTransform.localPosition = Vector3.zero + new Vector3(1, 0, 0); fill.rectTransform.anchorMax = new Vector2(0.0f, 0.5f); fill.rectTransform.anchorMin = new Vector2(0.0f, 0.5f); fill.enabled = true; SetFillRate(enemy.Status.HP.CurrentHealthRate); isInitialized = true; }
public void RegisterEnemy(Unit_AI enemy) { if (registeredEnemyDic == null) { registeredEnemyDic = new Dictionary <int, Unit_AI>(); } var instanceID = enemy.GetInstanceID(); if (registeredEnemyDic.ContainsKey(instanceID) == true) { return; } registeredEnemyDic.Add(instanceID, enemy); var targetHP = new GameObject($"{nameof(UI_Battle_TargetHP)}-{enemy.name}"); targetHP.AddComponent <RectTransform>(); targetHP.transform.SetParent(transform); var comp = targetHP.gameObject.AddComponent <UI_Battle_TargetHP_Child>(); comp.Initialize(enemy, Deregister); }