异步加载,球型检测,扇形攻击,持久化存储案例

建立四个场景,Building出来

S0场景第一个空物体S0Script挂脚本S0Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;//切换场景引用

public class S0Script : MonoBehaviour {

	//按下键盘q键,向持久化里面写入数据 张三,0
	//如果这个时候有张三这个key就代表已经有分数就不设置了 或里面的value不是0
	// Use this for initialization

	public string UserName;
	void Awake()
	{

		UserName = "zhangsan";
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown(KeyCode.Q))
		{
			if (PlayerPrefs.HasKey("zhangsan"))//持久化存储.HasKey判断是否存在
			{
				Debug.LogWarning("已经注册,该操作不会重置分数");
			}
			else
			{
				PlayerPrefs.SetInt("zhangsan",0);//SetInt写入类型Int
			}
		}

		if (Input.GetKeyDown(KeyCode.Space))
		{
			//加载场景
			//index string 
			SceneManager.LoadScene("S1");
		}
		
	}
}

第二个空物体S0UserInfo挂脚本S0UserInfo

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

public class S0UserInfo : MonoBehaviour {

	// Use this for initialization
	private string _username;

	public string Username//调用Username,返回_username
	{
		get { return _username; }
	}

	void Start ()
	{
		_username = GameObject.Find("Manager").GetComponent<S0Script>().UserName;//定义_username为S0Script的UserName,
		DontDestroyOnLoad(this);//加载场景不销毁.

	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

S1场景添加个Button,再建立个Manager控制,脚本如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using  UnityEngine.UI;
using  UnityEngine.SceneManagement;

public class S1Manager : MonoBehaviour {

	// Use this for initialization
	private Button _goBtn;
	void Start ()
	{

		_goBtn =	GameObject.Find("Button").GetComponent<Button>();
		_goBtn.onClick.AddListener(GoBtnOnClick);//动态监听事件

	}
	
	// Update is called once per frame
	void Update () {
		
	}



	private void GoBtnOnClick()
	{
		SceneManager.LoadScene("S2loding");
	}
}

创建一个Slider当进度条,再创建一个S2Manager来控制异步加载

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class S2Manager : MonoBehaviour
{



//UI进度条
	private Slider _proSlider;
//目的是对场景进行控制 获取进度值 和允许显示
	private AsyncOperation _async;
//UI当前达到的进度
	private int _currProgress;
	//1. 获取滑动条
	//协同加载(异步加载 不断获取进度值 经过计算赋值给滑动条)
	// Use this for initialization
	void Start ()
	{
		_currProgress = 0;
		_async = null;
		_proSlider = GameObject.Find("Slider").GetComponent<Slider>();
		StartCoroutine("LoadScene");
	}
	
	// Update is called once per frame
	void Update ()
	{
		//目的就是显示进度
		_proSlider.value = _currProgress / 100.0f;
	}

	IEnumerator LoadScene()
	{
//临时的进度
		int tmp;
		//异步加载
		_async = SceneManager.LoadSceneAsync("S3");

		//先不显示场景 等到进度为100%的时候显示场景 必须的!!!!
		_async.allowSceneActivation = false;

		#region 优化进度的 
		while (_async.progress < 0.9f)
		{
			//相当于滑动条应该到的位置
			tmp = (int) _async.progress * 100;
			
			//当滑动条 < tmp 就意味着滑动条应该变化
			while (_currProgress < tmp)
			{

				++_currProgress;
				yield return  new WaitForEndOfFrame();
			}
		}//while end   进度为90%

		tmp = 100;
		while (_currProgress < tmp)
		{

			++_currProgress;
			yield return  new WaitForEndOfFrame();
		}
		

		#endregion
		//处理进度为0 ~0.9的0
		
		//进度条完成 允许显示
		_async.allowSceneActivation = true;
		//SceneManager.loa
	}
}

S3场景创建S3Manager实现持久化存储分数

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

public class S3Manager : MonoBehaviour {

	// Use this for initialization
	private int _tmpScore;//分数
	private string _userName;
	void Start ()
	{
		_userName = GameObject.Find("UserInfo").GetComponent<S0UserInfo>().Username;

	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown(KeyCode.A))
		{
			//先假定已知zhangsna
			_tmpScore = PlayerPrefs.GetInt(_userName);//赋值,获取到_userName的值赋予_tmpScore,之前设置的是0;
			_tmpScore++;
			PlayerPrefs.SetInt(_userName,_tmpScore);
		}

		if (Input.GetKeyDown(KeyCode.S))
		{
			Debug.LogError(PlayerPrefs.GetInt(_userName));
		}
		
		
		
	}
}

S4场景实现球型检测与扇形攻击

玩家脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//扇形攻击

public class S4Player : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown(KeyCode.P))
		{
			AttackEnemy();
		}
		
	}


	void AttackEnemy()//球型检测
	{
		//获取在圈圈里面的人物
	Collider[] _colliders=	Physics.OverlapSphere(transform.position, 5);//固定格式,碰撞检测到的(自身位置,半径)
		//   符合要求的敌人列表
		List<GameObject> AttackdList = new List<GameObject>();
		//筛选完毕
		foreach (var v in _colliders)
		{
			//判断是否在攻击角度里面 
			if (UmbrellaAttact(transform, v.transform, 60, 5))//调用扇形攻击,返回bool值.
			{
				AttackdList.Add(v.gameObject);//加进列表内
				Debug.LogError(v.name);
			}
		}
//进行伤害计算
		foreach (GameObject o in AttackdList)
		{
			//获取距离
			float dis = Vector3.Distance(transform.position, o.transform.position);
//计算减血数值多少
			float blood = 5 * 10 / dis;
			
			//进行减血
			o.GetComponent<S4Enemy>().Attacked(blood);

		}

	}
	
	private bool UmbrellaAttact( Transform  attacker ,Transform attacked  ,float  angle, float radius)//扇形攻击
	{
        
		Vector3 deltaA = attacked.position - attacker.position;
        
		float tmpAngle = Mathf.Acos(Vector3.Dot(deltaA.normalized, attacker.forward)) * Mathf.Rad2Deg;
        
		if (tmpAngle < angle * 0.5f && deltaA.magnitude < radius)
		{
			return true;
		}
		return false;
	}

}

敌人脚本

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

public class S4Enemy : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	public void Attacked(float Attackedblood)
	{
		Debug.LogWarningFormat("我是{0} 我掉血{1}",name,Attackedblood);
		
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值