项目00《游戏-【02完结】-开发》Unity3D

首先打开项目

在Item下添加一个c#脚本Package

将代码复制

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class Package
{
    private List<ItemDate> _items = new List<ItemDate>();
    public void AddItem(ItemDate item)
    {
        if (_items.Count <= 0)
        { 
            _items.Add(item);
            return;
        }

        if (item.isStackable)
        {
            foreach (var i in _items)
            {
                if (i.name.Equals(item))
                {
                    i.amount += item.amount;
                    return;
                }
            }

            _items.Add(item);
            return;
        }
    }

}
 

 在Item包下创建包ItemTemplate,在包下创建c#脚本ItemDate

将以下代码复制

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
/// <summary>
/// 道具的数据封装基类
/// </summary>
public class ItemDate 
{
    //道具名
    public string name;
    //数量
    public int amount;
    //是否可堆叠
    public bool isStackable;
    //图标路径
    public string iconPath;
    //描述
    public string describe;
}

再创建c#脚本BaseItem 

将以下代码复制

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseItem : MonoBehaviour
{
    public ItemDate itemData;
    public void OnPickUp()
    { 
        GetComponent<MeshRenderer>().enabled = false;
        GetComponent<Collider>().enabled = false;
    }
}
在Canvas下创建Panel

改名为PackagePanel

 给PackagePanel添加一个垂直布局组

调整属性面板,勾选Width是为了控制宽度

 在PackagePanel下再创建一个Panel

命名为TiltlePanel

 

 再在TiltlePanel下创建Text(TMP)

 

 

将Text文本锚点调至左端,调节x位置

 属性面板调至为居中

在Package下再创建一个Panel

 改名为Content

给Content添加一个 H L Group

 调整其属性面板  设置控制宽度 和水平居中

接下来在内容Content下创建一个滑动窗口

 为其属性面板设置宽度

再创建一个滑动窗口

将水平hor设置成没有

 下面的也关掉

将水平滑动也关掉Horizontal

第二个也是

接着删掉这两个

如下操作

修改名称

对应加组件

 

添加一个Content Size Fitler

将垂直调成优化大小

同样上面的Content也加一下优化大小

修改名字

加水平布局 

 设置属性面板高度相等

再添加Image

将Content添加一个H L G,控制宽度

 然后在ItemSoft下调节属性面板高度改为60

接下来创建两个

调节属性面板 内容

 调节属性面板 数量

修改名字

接下来为ItemSlot写脚本

将以下代码复制

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ItemSlot:MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
{
    private Button _itemButton;
    private TMP_Text _itemName;
    private TMP_Text _itemAmount;
    private Image _itemIcon;
    private ItemDate _item;
    public ItemDate Item { get => _item;set => _item = value; } 


    public void OnPointerEnter(PointerEventData eventData)
    {
        throw new System.NotImplementedException();
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        throw new System.NotImplementedException();
    }

    private void Awake()
    {
        _itemButton = GetComponent<Button>();
        _itemName = transform.Find("ItemNameLabel").GetComponent<TMP_Text>();
        _itemAmount = transform.Find("ItemAmountLabel").GetComponent<TMP_Text>();
        _itemIcon = transform.Find("IconImage").GetComponent<Image>();
        
    }

    private void Update()
    {
        if (_item != null)
        {
            _itemName.text = _item.name;
            _itemAmount.text = _item.amount.ToString();
            _itemIcon.sprite = Resources.Load<Sprite>(_item.iconPath);
        }
    }
}
注意:

点左侧实现接口才不会报错

最后将脚本挂载到ItemSlot上

 

 首先新建一个脚本PackagePanel

 将代码挂载在PackagePanel上

将以下代码复制PackagePanel

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PackagePanel : MonoBehaviour
{
    private Transform _itemContent;
    private Transform _itemDescribe;
    public Transform ItemDescribe { get => _itemDescribe; set => _itemDescribe = value; }
    private void Awake()
    {
        _itemContent = transform.Find("ItemView");
        _itemDescribe = transform.Find("ItemAmountLabel");
    }
}
 

并修改ItemSlot代码

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ItemSlot:MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
{
    private PackagePanel _root => GetComponentInParent<PackagePanel>();

    private Button _itemButton;
    private TMP_Text _itemName;
    private TMP_Text _itemAmount;
    private Image _itemIcon;
    private ItemDate _item;
    public ItemDate Item { get => _item;set => _item = value; } 


   

    private void Awake()
    {
        _itemButton = GetComponent<Button>();
        _itemName = transform.Find("ItemNameLabel").GetComponent<TMP_Text>();
        _itemAmount = transform.Find("ItemAmountLabel").GetComponent<TMP_Text>();
        _itemIcon = transform.Find("IconImage").GetComponent<Image>();
        
    }

    private void Update()
    {
        if (_item != null)
        {
            _itemName.text = _item.name;
            _itemAmount.text = _item.amount.ToString();
            _itemIcon.sprite = Resources.Load<Sprite>(_item.iconPath);
        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        _root.ItemDescribe.GetComponent<TMP_Text>().text = _item.describe;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        _root.ItemDescribe.GetComponent<TMP_Text>().text = "";
    }
}

给PackagePanel 添加组件 Canvas Group

 并且修改PackagePanel代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PackagePanel : MonoBehaviour
{
    private Transform _itemContent;
    private Transform _itemDescribe;
    public Transform ItemDescribe { get => _itemDescribe; set => _itemDescribe = value; }

    public bool isPanelOpend; //开关标记

    private CanvasGroup _canvasGroup; //引用

    private void Awake()
    {
        _itemContent = transform.Find("ItemView");
        _itemDescribe = transform.Find("ItemAmountLabel");
        _canvasGroup = GetComponent<CanvasGroup>();
    }

    private void Update()
    {
        if (Input.GetKeyUp(KeyCode.B))
        { 
            isPanelOpend = !isPanelOpend;
            if (isPanelOpend)
            { 
                _canvasGroup.alpha = 1.0f;
                _canvasGroup.blocksRaycasts = true;
            }
        }
    }

}

将ItemSlot 放置预制体

之后再修改Package代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class Package
{
    private List<ItemDate> _items = new List<ItemDate>();
    public void AddItem(ItemDate item)
    {
        if (_items.Count <= 0)
        { 
            _items.Add(item);
            return;
        }

        if (item.isStackable)
        {
            foreach (var i in _items)
            {
                if (i.name.Equals(item))
                {
                    i.amount += item.amount;
                    return;
                }
            }

            _items.Add(item);
            return;
        }
    }

    public List<GameObject> GetItemList() 
    {
        if (_items.Count <= 0)
            return null;
        var result = new List<GameObject>();
        foreach (var item in _items)
        {
            var itemObj = GameObject.Instantiate(Resources.Load<GameObject>
                ("Prefabs/UIItems/ItemSolt"));
            itemObj.GetComponent<ItemSlot>().Item = item;
            result.Add(itemObj);
        }
        return result;
    }

}
 

修改player脚本


using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Scripting.APIUpdating;
public static class AnimatorList
{

    public static int IsIdle = Animator.StringToHash("isIdle");
    public static int IsWalk = Animator.StringToHash("isWalk");
    public static int IsRun = Animator.StringToHash("isRun");
    public static int IsDying = Animator.StringToHash("isDying");
}

public class Player : MonoBehaviour
{
    public GameObject WinPanel;

    private CharacterController _characterController;
    private Animator _animator;
    public float velocity = 5f;
    public float health = 100;
    //public int package = 0;
    public int condition = 1;
    

    //后添加
    public Package packageScript;

    private void Awake()
    {
        _characterController = GetComponent<CharacterController>();
        _animator = GetComponent<Animator>();
        //后添加
        packageScript = new Package();
    }


    void Start()
    {
        _animator.SetBool(AnimatorList.IsIdle, true);
    }


    void Update()
    {
        Move();
        MoveByAnimation();
        PickUpItem();
        OpenDoor();
    }
    private void Move()
    {
        var vertical = transform.forward * Input.GetAxis("Vertical") * velocity * Time.deltaTime;
        var horizontal = transform.right * Input.GetAxis("Horizontal") * velocity * Time.deltaTime;
        Vector3 gravity = Vector3.zero;
        if (!_characterController.isGrounded)
            gravity = transform.up * -9.8f;

        _characterController.Move(horizontal + vertical + gravity);
        /*Vector3 moveVector;   
        Vector3 moveVector;
        moveVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
       
        Vector3 gravity = Vector3.zero;
        if (!_characterController.isGrounded)
            gravity = transform.up * 9.8f;
        moveVector += gravity;
        moveVector *= Time.deltaTime;
        _characterController.Move(moveVector);*/
    }
    //计算移动方向
    private void MoveByAnimation()
    {
        if (!_animator.GetBool(AnimatorList.IsWalk) && !_animator.GetBool(AnimatorList.IsRun))
        {
            _animator.SetBool(AnimatorList.IsIdle, true);
        }
        if (Input.GetAxis("Vertical") != 0 && !_animator.GetBool(AnimatorList.IsRun))
        {
            _animator.SetBool(AnimatorList.IsIdle, false);
            _animator.SetBool(AnimatorList.IsWalk, true);
            _animator.SetFloat("velocity", Input.GetAxis("Vertical"));

        }
        if (_animator.GetFloat("velocity") < 0.2f)
        {
            _animator.SetBool(AnimatorList.IsIdle, true);
            _animator.SetBool(AnimatorList.IsWalk, false);
            _animator.SetBool(AnimatorList.IsRun, false);
        }
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            _animator.SetBool(AnimatorList.IsIdle, false);
            _animator.SetBool(AnimatorList.IsWalk, false);
            _animator.SetBool(AnimatorList.IsRun, true);
        }
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {

            _animator.SetBool(AnimatorList.IsRun, false);
        }
    }
    public void OpenDoor()
    {
        if (Input.GetKeyUp(KeyCode.E))
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out var hit))
            {
                if (hit.collider.CompareTag("Door"))
                {
                    //if (package == condition && package != 0)
                   // {


                        WinPanel.SetActive(true);
                        //Time.timeScale = 0.1f;
                        StartCoroutine(OnGameEnd());
                   // }
                    //胜利
                }
            }
        }
    }
    public void PickUpItem()
    {
        if (Input.GetKeyUp(KeyCode.E))
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out var hit))
            {
                if (hit.collider.CompareTag("Item"))
                {
                   // package += hit.collider.GetComponent<BoxKey>().id;
                    Destroy(hit.collider.gameObject);
                }
            }
        }
    }

    public IEnumerator OnGameEnd()
    {
        yield return new WaitForSeconds(3);
        SceneManager.LoadScene("EntyScene");
    }
}


 修改Package脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class Package
{
    private List<ItemDate> _items = new List<ItemDate>();
    public void AddItem(ItemDate item)
    {
        if (_items.Count <= 0)
        { 
            _items.Add(item);
            return;
        }

        if (item.isStackable)
        {
            foreach (var i in _items)
            {
                if (i.name.Equals(item))
                {
                    i.amount += item.amount;
                    return;
                }
            }

            _items.Add(item);
            return;
        }
    }

    public List<GameObject> GetItemList() 
    {
        if (_items.Count <= 0)
            return null;
        var result = new List<GameObject>();
        foreach (var item in _items)
        {
            var itemObj = GameObject.Instantiate(Resources.Load<GameObject>
                ("Prefabs/UIItems/ItemSlot"));
            itemObj.GetComponent<ItemSlot>().Item = item;
            result.Add(itemObj);
        }
        return result;
    }

}

修改PackagePanel脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PackagePanel : MonoBehaviour
{
    private Transform _itemContent;
    private Transform _itemDescribe;
    public Transform ItemDescribe { get => _itemDescribe; set => _itemDescribe = value; }

    public bool isPanelOpened; //开关标记

    private CanvasGroup _canvasGroup; //引用

    private void Awake()
    {
        _itemContent = transform.Find("ItemView");
        _itemDescribe = transform.Find("DescribeView");
        _canvasGroup = GetComponent<CanvasGroup>();
    }

    private void Update()
    {
        if (Input.GetKeyUp(KeyCode.B))
        {
            if (_canvasGroup.alpha > 0)
            {
                Cursor.lockState = CursorLockMode.None;
                Time.timeScale = 0.1f;
            }
            else
            {
                Cursor.lockState = CursorLockMode.Locked;
                Time.timeScale = 1;
            }
            if (isPanelOpened)
            {
                isPanelOpened = !isPanelOpened;
                _canvasGroup.alpha = 1;
                _canvasGroup.blocksRaycasts = true;
                //后添加修改
                var itemList = GameObject.FindGameObjectWithTag(
                    "Player").GetComponent<Player>().packageScript.GetItemList();
                if (itemList != null)
                {
                    foreach (var item in itemList)
                    {
                        item.transform.parent = _itemContent;
                    }
                }
            }
            else
            { 
                 ClearChilds();
                _canvasGroup.blocksRaycasts = false;
            }
        }
    }

    private void ClearChilds()
    {
        if (_itemContent.childCount <= 0)
            return;
        foreach (var item in _itemContent.GetComponentsInChildren<Transform>())
        {
            if (item != _itemContent)
            {
                Destroy(item.gameObject);
            }
        }
    }

}

 接下来写一个Key脚本

将以下代码复制

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Asseys.Scripts.GamePlay.Items.ItemTempLate.ItemInsance
{
    public class Key : BaseItem
    {
        private void Start()
        {
            itemData.name = "Key";
            itemData.amount = 1;
            itemData.isStackable = false;
            itemData.describe = "This is a key";
            itemData.iconPath = "";
        }
    }

}

将Itemsolt删掉

修改属性

将PackagePanel设置为预制体

将PackagePanel属性面板设置成 Alpha 0

将Package删除

 之后进入游戏场景

 更换代码绑定

 

将面板放在Canvas下

 设置0

更换Plater代码


using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Scripting.APIUpdating;
public static class AnimatorList
{

    public static int IsIdle = Animator.StringToHash("isIdle");
    public static int IsWalk = Animator.StringToHash("isWalk");
    public static int IsRun = Animator.StringToHash("isRun");
    public static int IsDying = Animator.StringToHash("isDying");
}

public class Player : MonoBehaviour
{
    public GameObject WinPanel;

    private CharacterController _characterController;
    private Animator _animator;
    public float velocity = 5f;
    public float health = 100;
    //public int package = 0;
    public int condition = 1;
    

    //后添加
    public Package packageScript;

    private void Awake()
    {
        _characterController = GetComponent<CharacterController>();
        _animator = GetComponent<Animator>();
        //后添加
        packageScript = new Package();
    }


    void Start()
    {
        _animator.SetBool(AnimatorList.IsIdle, true);
    }


    void Update()
    {
        Move();
        MoveByAnimation();
        PickUpItem();
        OpenDoor();
    }
    private void Move()
    {
        var vertical = transform.forward * Input.GetAxis("Vertical") * velocity * Time.deltaTime;
        var horizontal = transform.right * Input.GetAxis("Horizontal") * velocity * Time.deltaTime;
        Vector3 gravity = Vector3.zero;
        if (!_characterController.isGrounded)
            gravity = transform.up * -9.8f;

        _characterController.Move(horizontal + vertical + gravity);
        /*Vector3 moveVector;   
        Vector3 moveVector;
        moveVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
       
        Vector3 gravity = Vector3.zero;
        if (!_characterController.isGrounded)
            gravity = transform.up * 9.8f;
        moveVector += gravity;
        moveVector *= Time.deltaTime;
        _characterController.Move(moveVector);*/
    }
    //计算移动方向
    private void MoveByAnimation()
    {
        if (!_animator.GetBool(AnimatorList.IsWalk) && !_animator.GetBool(AnimatorList.IsRun))
        {
            _animator.SetBool(AnimatorList.IsIdle, true);
        }
        if (Input.GetAxis("Vertical") != 0 && !_animator.GetBool(AnimatorList.IsRun))
        {
            _animator.SetBool(AnimatorList.IsIdle, false);
            _animator.SetBool(AnimatorList.IsWalk, true);
            _animator.SetFloat("velocity", Input.GetAxis("Vertical"));

        }
        if (_animator.GetFloat("velocity") < 0.2f)
        {
            _animator.SetBool(AnimatorList.IsIdle, true);
            _animator.SetBool(AnimatorList.IsWalk, false);
            _animator.SetBool(AnimatorList.IsRun, false);
        }
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            _animator.SetBool(AnimatorList.IsIdle, false);
            _animator.SetBool(AnimatorList.IsWalk, false);
            _animator.SetBool(AnimatorList.IsRun, true);
        }
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {

            _animator.SetBool(AnimatorList.IsRun, false);
        }
    }
    public void OpenDoor()
    {
        if (Input.GetKeyUp(KeyCode.E))
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out var hit))
            {
                if (hit.collider.CompareTag("Door"))
                {
                    //if (package == condition && package != 0)
                   // {


                        WinPanel.SetActive(true);
                        //Time.timeScale = 0.1f;
                        StartCoroutine(OnGameEnd());
                   // }
                    //胜利
                }
            }
        }
    }
    public void PickUpItem()
    {
        if (Input.GetKeyUp(KeyCode.E))
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out var hit))
            {
                if (hit.collider.CompareTag("Item"))
                {
                    // package += hit.collider.GetComponent<BoxKey>().id;
                    packageScript.AddItem(hit.collider.GetComponent<BaseItem>().itemData);
                    Destroy(hit.collider.gameObject);
                }
            }
        }
    }

    public IEnumerator OnGameEnd()
    {
        yield return new WaitForSeconds(3);
        SceneManager.LoadScene("EntyScene");
    }
}


 更换Key代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Asseys.Scripts.GamePlay.Items.ItemTempLate.ItemInsance
{
    public class Key : BaseItem
    {
        private void Start()
        {
            itemData = new ItemDate();
            itemData.name = "Key";
            itemData.amount = 1;
            itemData.isStackable = false;
            itemData.describe = "This is a key";
            itemData.iconPath = "";
        }
    }

}

最后更新代码

Player代码更新如下


using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Scripting.APIUpdating;
public static class AnimatorList
{

    public static int IsIdle = Animator.StringToHash("isIdle");
    public static int IsWalk = Animator.StringToHash("isWalk");
    public static int IsRun = Animator.StringToHash("isRun");
    public static int IsDying = Animator.StringToHash("isDying");
}

public class Player : MonoBehaviour
{
    public GameObject WinPanel;

    private CharacterController _characterController;
    private Animator _animator;
    public float velocity = 5f;
    public float health = 100;
    //public int package = 0;
    public int condition = 1;
    

    //后添加
    public Package packageScript;

    private void Awake()
    {
        _characterController = GetComponent<CharacterController>();
        _animator = GetComponent<Animator>();
        //后添加
        packageScript = new Package();
    }


    void Start()
    {
        _animator.SetBool(AnimatorList.IsIdle, true);
    }


    void Update()
    {
        Move();
        MoveByAnimation();
        PickUpItem();
        OpenDoor();
    }
    private void Move()
    {
        var vertical = transform.forward * Input.GetAxis("Vertical") * velocity * Time.deltaTime;
        var horizontal = transform.right * Input.GetAxis("Horizontal") * velocity * Time.deltaTime;
        Vector3 gravity = Vector3.zero;
        if (!_characterController.isGrounded)
            gravity = transform.up * -9.8f;

        _characterController.Move(horizontal + vertical + gravity);
        /*Vector3 moveVector;   
        Vector3 moveVector;
        moveVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
       
        Vector3 gravity = Vector3.zero;
        if (!_characterController.isGrounded)
            gravity = transform.up * 9.8f;
        moveVector += gravity;
        moveVector *= Time.deltaTime;
        _characterController.Move(moveVector);*/
    }
    //计算移动方向
    private void MoveByAnimation()
    {
        if (!_animator.GetBool(AnimatorList.IsWalk) && !_animator.GetBool(AnimatorList.IsRun))
        {
            _animator.SetBool(AnimatorList.IsIdle, true);
        }
        if (Input.GetAxis("Vertical") != 0 && !_animator.GetBool(AnimatorList.IsRun))
        {
            _animator.SetBool(AnimatorList.IsIdle, false);
            _animator.SetBool(AnimatorList.IsWalk, true);
            _animator.SetFloat("velocity", Input.GetAxis("Vertical"));

        }
        if (_animator.GetFloat("velocity") < 0.2f)
        {
            _animator.SetBool(AnimatorList.IsIdle, true);
            _animator.SetBool(AnimatorList.IsWalk, false);
            _animator.SetBool(AnimatorList.IsRun, false);
        }
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            _animator.SetBool(AnimatorList.IsIdle, false);
            _animator.SetBool(AnimatorList.IsWalk, false);
            _animator.SetBool(AnimatorList.IsRun, true);
        }
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {

            _animator.SetBool(AnimatorList.IsRun, false);
        }
    }
    public void OpenDoor()
    {
        if (Input.GetKeyUp(KeyCode.E))
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out var hit))
            {
                if (hit.collider.CompareTag("Door"))
                {
                    //if (package == condition && package != 0)
                   // {


                        WinPanel.SetActive(true);
                        //Time.timeScale = 0.1f;
                        StartCoroutine(OnGameEnd());
                   // }
                    //胜利
                }
            }
        }
    }
    public void PickUpItem()
    {
        if (Input.GetKeyUp(KeyCode.E))
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out var hit))
            {
                if (hit.collider.CompareTag("Item"))
                {
                    // package += hit.collider.GetComponent<BoxKey>().id;
                    packageScript.AddItem(hit.collider.GetComponent<BaseItem>().itemData);
                    Destroy(hit.collider.gameObject);
                }
            }
        }
    }

    public IEnumerator OnGameEnd()
    {
        yield return new WaitForSeconds(3);
        SceneManager.LoadScene("EntyScene");
    }
}


PackagePanel代码更新如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PackagePanel : MonoBehaviour
{
    private Transform _itemContent;
    private Transform _itemDescribe;
    public Transform ItemDescribe { get => _itemDescribe; set => _itemDescribe = value; }

    public bool isPanelOpened; //开关标记

    private CanvasGroup _canvasGroup; //引用

    private void Awake()
    {
        _itemContent = transform.Find("Content/ItemView/Viewport/Content");
        _itemDescribe = transform.Find("Content/DescribeView/Viewport/Content");
        _canvasGroup = GetComponent<CanvasGroup>();
    }

    private void Update()
    {
        if (_canvasGroup.alpha > 0)
        {
            Cursor.lockState = CursorLockMode.None;
            Time.timeScale = 0.1f;
        }
        else
        {
            Cursor.lockState = CursorLockMode.Locked;
            Time.timeScale = 1;
        }

        if (Input.GetKeyUp(KeyCode.B))
        {
            isPanelOpened = !isPanelOpened;
            if (isPanelOpened)
            {
                _canvasGroup.alpha = 1;
                _canvasGroup.blocksRaycasts = true;
                //后添加修改
                var itemList = GameObject.FindGameObjectWithTag(
                    "Player").GetComponent<Player>().packageScript.GetItemList();
                if (itemList != null)
                {
                    foreach (var item in itemList)
                    {
                        item.transform.parent = _itemContent;
                    }
                }
            }
            else
            { 
                 ClearChilds();
                _canvasGroup.alpha = 0;
                _canvasGroup.blocksRaycasts = false;
            }
        }
    }

    private void ClearChilds()
    {
        if (_itemContent.childCount <= 0)
            return;

        foreach (var item in _itemContent.GetComponentsInChildren<Transform>())
        {
            if (item != _itemContent)
            {
                Destroy(item.gameObject);
            }
        }
    }

}

接下来添加雾气

如下属性面板可修改 

 

即可完成,并完成背包系统

End.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值