2025-11-25 17:41:28 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace StudyCase3
|
|
|
|
|
{
|
|
|
|
|
public class Item : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
Outline outline;
|
2025-11-26 21:34:22 +08:00
|
|
|
bool pick;
|
|
|
|
|
ThirdCharacterController player;
|
2025-11-25 17:41:28 +08:00
|
|
|
private void Awake()
|
|
|
|
|
{
|
2025-11-26 21:34:22 +08:00
|
|
|
player = FindAnyObjectByType<ThirdCharacterController>();
|
2025-11-25 17:41:28 +08:00
|
|
|
if (outline == null)
|
|
|
|
|
outline = gameObject.AddComponent<Outline>();
|
|
|
|
|
else
|
|
|
|
|
outline = GetComponent<Outline>();
|
2025-11-26 21:34:22 +08:00
|
|
|
outline.enabled = false;
|
2025-11-25 17:41:28 +08:00
|
|
|
outline.OutlineMode = Outline.Mode.OutlineVisible;
|
2025-11-26 21:34:22 +08:00
|
|
|
outline.OutlineColor = new Color(0, 1, 1);
|
2025-11-25 17:41:28 +08:00
|
|
|
outline.OutlineWidth = 10f;
|
|
|
|
|
gameObject.layer = LayerMask.NameToLayer("Item");
|
|
|
|
|
}
|
2025-11-26 21:34:22 +08:00
|
|
|
public void Select()
|
|
|
|
|
{
|
|
|
|
|
if(!pick)
|
|
|
|
|
outline.enabled = true;
|
|
|
|
|
}
|
|
|
|
|
public void UnSelect()
|
|
|
|
|
{
|
|
|
|
|
if (!pick)
|
|
|
|
|
outline.enabled = false;
|
|
|
|
|
}
|
2025-11-25 17:41:28 +08:00
|
|
|
public void PickUp(Transform root)
|
|
|
|
|
{
|
2025-11-26 21:34:22 +08:00
|
|
|
if (pick) return;
|
2025-11-25 17:41:28 +08:00
|
|
|
pick = true;
|
2025-11-26 21:34:22 +08:00
|
|
|
transform.SetParent(root);
|
2025-11-25 17:41:28 +08:00
|
|
|
outline.OutlineWidth = 2f;
|
|
|
|
|
gameObject.layer = LayerMask.NameToLayer("Default");
|
2025-11-26 21:34:22 +08:00
|
|
|
player.SetItems();
|
2025-11-25 17:41:28 +08:00
|
|
|
}
|
2025-11-26 21:34:22 +08:00
|
|
|
private void OnMouseEnter()
|
2025-11-25 17:41:28 +08:00
|
|
|
{
|
2025-11-26 21:34:22 +08:00
|
|
|
if (player.pickMode == PickMode.Mouse)
|
|
|
|
|
Select();
|
2025-11-25 17:41:28 +08:00
|
|
|
}
|
2025-11-26 21:34:22 +08:00
|
|
|
private void OnMouseExit()
|
2025-11-25 17:41:28 +08:00
|
|
|
{
|
2025-11-26 21:34:22 +08:00
|
|
|
if (player.pickMode == PickMode.Mouse)
|
|
|
|
|
UnSelect();
|
2025-11-25 17:41:28 +08:00
|
|
|
}
|
2025-11-26 21:34:22 +08:00
|
|
|
private void OnMouseDown()
|
2025-11-25 17:41:28 +08:00
|
|
|
{
|
2025-11-26 21:34:22 +08:00
|
|
|
if (player.pickMode == PickMode.Mouse)
|
|
|
|
|
PickUp(player.itemRoot);
|
2025-11-25 17:41:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|