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

posted on 2017-02-24 13:36  void87  阅读(236)  评论(0编辑  收藏  举报

导航