• Roll a ball 学习


    using UnityEngine;
    using System.Collections;
    
    /// <summary>
    /// 相机控制
    /// </summary>
    public class CameraController : MonoBehaviour {
    
        /// <summary>
        /// 玩家
        /// </summary>
        public GameObject player;
    
        /// <summary>
        /// 玩家到相机的偏移
        /// </summary>
        private Vector3 offset;
    
    
        void Start ()
        {
            //计算偏移
            offset = transform.position - player.transform.position;
        }
    
        void LateUpdate ()
        {
            //设置相机的位置为玩家的位置加 玩家到相机的偏移
            transform.position = player.transform.position + offset;
        }
    }
    CameraController
    using UnityEngine;
    
    using UnityEngine.UI;
    
    using System.Collections;
    
    /// <summary>
    /// 玩家控制
    /// </summary>
    public class PlayerController : MonoBehaviour {
        
        /// <summary>
        /// 移动速度
        /// </summary>
        public float speed;
        /// <summary>
        /// 吃掉的球的数量的文本
        /// </summary>
        public Text countText;
        /// <summary>
        /// 显示赢得比赛的文本
        /// </summary>
        public Text winText;
    
        /// <summary>
        /// Rigidbody
        /// </summary>
        private Rigidbody rb;
        /// <summary>
        /// 玩家吃掉的球的数量
        /// </summary>
        private int count;
    
        void Start ()
        {
            rb = GetComponent<Rigidbody>();
    
            count = 0;
    
            SetCountText ();
    
            winText.text = "";
        }
    
        void FixedUpdate ()
        {
            float moveHorizontal = Input.GetAxis ("Horizontal");
            float moveVertical = Input.GetAxis ("Vertical");
    
            Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    
            rb.AddForce (movement * speed);
        }
    
        void OnTriggerEnter(Collider other) 
        {
            if (other.gameObject.CompareTag ("Pick Up"))
            {
                other.gameObject.SetActive (false);
    
                count = count + 1;
    
                SetCountText ();
            }
        }
    
        /// <summary>
        /// 设置数量文本
        /// </summary>
        void SetCountText()
        {
            //更新显示数量的文本
            countText.text = "Count: " + count.ToString ();
    
            //如果数量大于等于12,
            if (count >= 12) 
            {
                winText.text = "You Win!";
            }
        }
    }
    PlayerController
    using UnityEngine;
    using System.Collections;
    
    /// <summary>
    /// 旋转小球
    /// </summary>
    public class Rotator : MonoBehaviour {
    
        void Update () 
        {
            transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime);
        }
    }    
    Rotator
    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    
    
    /// <summary>
    /// 教程信息
    /// </summary>
    public class TutorialInfo : MonoBehaviour 
    {
        /// <summary>
        /// 是否在开始时显示
        /// </summary>
        public bool showAtStart = true;
    
        /// <summary>
        /// 链接地址
        /// </summary>
        public string url;
    
        /// <summary>
        /// 覆盖层
        /// </summary>
        public GameObject overlay;
    
        public AudioListener mainListener;
    
        /// <summary>
        /// 开关 控制是否在开始时显示
        /// </summary>
        public Toggle showAtStartToggle;
    
        /// <summary>
        /// showLaunchScreen PlayerPrefs
        /// </summary>
        public static string showAtStartPrefsKey = "showLaunchScreen";
    
        /// <summary>
        /// 是否已经显示过了
        /// </summary>
        private static bool alreadyShownThisSession = false;
    
    
        void Awake()
        {
            if(alreadyShownThisSession)
            {
                StartGame();
            }
            else
            {
                alreadyShownThisSession = true;
    
                //如果prefs 有键,获取值,赋值给 showAtStart变量
                if (PlayerPrefs.HasKey(showAtStartPrefsKey))
                {
                    showAtStart = PlayerPrefs.GetInt(showAtStartPrefsKey) == 1;
                }
    
                //根据showAtStart 设置 Toggle 的勾选状态
                showAtStartToggle.isOn = showAtStart;
    
                //根据showAtStart 显示启动界面或者直接开始游戏
                if (showAtStart) 
                {
                    ShowLaunchScreen();
                }
                else 
                {
                    StartGame ();
                }    
            }
        }
    
        /// <summary>
        /// 显示启动界面
        /// </summary>
        public void ShowLaunchScreen()
        {
            //游戏暂停
            Time.timeScale = 0f;
            mainListener.enabled = false;
            overlay.SetActive (true);
        }
    
        /// <summary>
        /// 打开链接地址
        /// </summary>
        public void LaunchTutorial()
        {
            Application.OpenURL (url);
        }
    
        /// <summary>
        /// 开始游戏
        /// </summary>
        public void StartGame()
        {        
            overlay.SetActive (false);
            mainListener.enabled = true;
            //恢复正常的时间
            Time.timeScale = 1f;
        }
    
        /// <summary>
        /// 打开/关闭 是否在启动时显示
        /// </summary>
        public void ToggleShowAtLaunch()
        {
            showAtStart = showAtStartToggle.isOn;
            PlayerPrefs.SetInt(showAtStartPrefsKey, showAtStart ? 1 : 0);
        }
    }
    TutorialInfo
    using UnityEngine;
    using UnityEditor;
    using System.Collections;
    
    /// <summary>
    /// 教程信息编辑器
    /// </summary>
    [CustomEditor(typeof(TutorialInfo))]    //指定这个编辑器脚本对应的脚本
    public class TutorialInfoEditor : Editor 
    {
        /// <summary>
        /// ScriptObject.OnEnable
        /// </summary>
        void OnEnable()
        {
            //如果首选项里存在键
            if (PlayerPrefs.HasKey(TutorialInfo.showAtStartPrefsKey))
            {
                //读取值,根据值设置是否在开始时启用
                ((TutorialInfo)target).showAtStart = PlayerPrefs.GetInt(TutorialInfo.showAtStartPrefsKey) == 1;
            }
        }
    
        /// <summary>
        /// Editor.OnInSpectorGUI
        /// 自定义检视面板
        /// </summary>
        public override void OnInspectorGUI()
        {
            //检查BeginChangeCheck()/EndChangeCheck() 里代码块内的控件是否有变
            EditorGUI.BeginChangeCheck();
    
            base.OnInspectorGUI();
    
            //如果有变化,重新获取并设置 showAtStartPrefsKey 的值
            if(EditorGUI.EndChangeCheck()) {
                PlayerPrefs.SetInt(TutorialInfo.showAtStartPrefsKey,((TutorialInfo)target).showAtStart ? 1 : 0);
            }
        }
    }
    TutorialInfoEditor

    视频:https://pan.baidu.com/s/1jIHlTga

    项目:https://pan.baidu.com/s/1c1Tffo

  • 相关阅读:
    关于联想笔记本ThinkPad E470 没有外音 插耳机却有声音的解决办法
    Win10无法启动软件提示MSVCP110.dll丢失
    POJ-3984 迷宫问题(BFS找最短路径并保存)
    转圈游戏(简单的快速幂)
    统计一个整数的二进制中1的个数(暴力)
    手写哈希(实现简单的加数、查询)
    CodeForces
    L2-2 社交集群 (25 分)(一个写挫的并查集)
    7-4 交换二叉树中每个结点的左孩子和右孩子 (20 分)
    7-3 堆中的路径 (25 分)
  • 原文地址:https://www.cnblogs.com/revoid/p/6438214.html
Copyright © 2020-2023  润新知