美文网首页
2018-03-08-液体的吸取与添加案例(练习Unity中的D

2018-03-08-液体的吸取与添加案例(练习Unity中的D

作者: SmallFlydream | 来源:发表于2018-03-08 19:30 被阅读0次

1.自己写一个ReagentContainerBase作为液体容器类(也是装液体容器的父类)

2.子类Cup继承ReagentContainerBase,在start函数里初始化杯子里的液体

3.移液器Pipettor类在start函数里找到杯子(cup),然后利用触发器在移液器进入杯子(cup)类时减少杯子(cup)里的液体

1.ReagentContainerBase脚本不用挂在物体上

注意在生命Dictionary<string, int>类时必须初始化(实例化一个对象),否则容易报空值错
  
using  UnityEngine;    
using  System.Collections;
using System.Collections.Generic;

namespace BioVR
{
    public class ReagentContainerBase : MonoBehaviour  
    {
        private Reagents reagent =new Reagents();
        /// <summary>
        /// 试剂容器的液体
        /// </summary>
        public Dictionary<string, int> ReagentContainer = new Dictionary<string, int>();
        public Dictionary<string, int> NewReagentContainer = new Dictionary<string, int>();

        /// <summary>
        /// 添加溶液的方法 
        /// </summary>
        /// <param name="reagentName"></param>
        /// <param name="reagentVolume"></param>
        public void AddReagent(string reagentName,int reagentVolume) {
            
            ReagentContainer.Add(reagentName, reagentVolume);
            reagent.ReagentsName="";
            reagent.ReagentsVolume = 0;
            Dictionary<string, int>.KeyCollection keyCol = ReagentContainer.Keys;
            foreach (string item in keyCol)
            {
                reagent.ReagentsName += item;
                reagent.ReagentsVolume += ReagentContainer[item];
               
            }
            ReagentContainer.Clear();
            ReagentContainer.Add(reagent.ReagentsName, reagent.ReagentsVolume);
            Debug.Log(reagent.ReagentsName+":"+ ReagentContainer[reagent.ReagentsName]);
        }
        public void ReduceReagent(int reagentVolume) {
            reagent.ReagentsVolume -= reagentVolume;
            ReagentContainer.Clear();
            NewReagentContainer.Add(reagent.ReagentsName, reagentVolume);
            ReagentContainer.Add(reagent.ReagentsName, reagent.ReagentsVolume);
            Debug.Log("减少了" + reagent.ReagentsName + NewReagentContainer[reagent.ReagentsName] + "原来还剩下" + ReagentContainer[reagent.ReagentsName]);
        }

    }  
}


2.cup脚本挂在一个杯子物体上

using UnityEngine;
using System.Collections;
using System;

namespace BioVR
{
    public class Cup : ReagentContainerBase
    {
        //调用类的方法
        private void Start()
        {
            AddReagent("可乐", 45);
            AddReagent("雪碧", 45);
        }
       
    }
}

3.Pipettor挂在移液器物体上

实际移液器也应该继承ReagentContainerBase类,他也可以状液体和排液体,还有一些细节问题到时候自己在加吧
using  UnityEngine;    
using  System.Collections;    
    
namespace BioVR
{
    public class Pipettor : MonoBehaviour  
    {
        Cup cup;
        private void Start()
        {
            cup = GameObject.Find("Cup").GetComponent<Cup>();
        }
        private void OnTriggerEnter(Collider other)
        {
            cup.ReduceReagent(15);
        }
    }  
}

相关文章

网友评论

      本文标题:2018-03-08-液体的吸取与添加案例(练习Unity中的D

      本文链接:https://www.haomeiwen.com/subject/ibayfftx.html