- 最后登录
- 2019-12-25
- 注册时间
- 2012-8-24
- 阅读权限
- 90
- 积分
- 71088
- 纳金币
- 52336
- 精华
- 343
|
一个gameobject下有多个代码组件,不同代码里不同的函数可以用sendmessage调用,传参。还有种情况,两个不同的游戏对象挂载同一个代码,一片代码里用枚举类型的来区分不同的gameobject,两个不同的gameobjec去调用某些方法时,也需要用sendmessage,这是今天的一部分代码。很感谢带我的人给我的指导。
public enum AIType
{
玩家,
敌人
}
public AIType aiType = AIType.玩家;
//玩家控制
public PlayerControl player;
//ai控制
public EnemyControl enemy;
//淹死的位置
public Transform w_down;
//敌人
public GameObject[] enemys;
public TextMesh showSkill;
private float old_speed = 0;
private float skillColdTime = 12.0f;
private float tempTime = 0;
// Use this for initialization
private GameObject playerobj;
void Start ()
{
if (aiType == AIType.玩家)
old_speed = player.Move_Speed;
else {
playerobj = GameObject.FindWithTag ("playerbody");
old_speed = enemy.Move_Speed;
}
}
// Update is called once per frame
float dis;
void Update ()
{
if (aiType == AIType.敌人) {
dis = Vector3.Distance (playerobj.transform.position, transform.position);
// Debug.Log (dis);
if (dis < 15) {
if (Time.time > tempTime + Random.Range (5, 10)) {
tempTime = Time.time + skillColdTime;
StartCoroutine ("ReleaseSkill", Random.Range (100, 110));
}
}
}
}
/// <summary>
/// Skill the specified Type_ and skill_id.
/// </summary>
/// <param name="Type_">Type_.</param>
/// <param name="skill_id">Skill_id.</param>
public void Skill (string str)
{
string[] strs = str.Split ('_');
switch (strs [0]) {
case "rel"://释放技能
StartCoroutine ("ReleaseSkill", int.Parse (strs [1]));
break;
case "get"://得到技能状态
StopCoroutine ("GetSkillAction");
StartCoroutine ("GetSkillAction", int.Parse (strs [1]));
break;
default:
break;
}
}
/// <summary>
/// 触发技能
/// </summary>
/// <param name="skill_id">Skill_id.</param>
IEnumerator ReleaseSkill (int skill_id)
{
switch (skill_id) {
case 101://反向
if (aiType == AIType.敌人)
playerobj.SendMessage ("Skill", "get_101");
break;
case 102://减速
if (aiType == AIType.敌人)
playerobj.SendMessage ("Skill", "get_102");
break;
case 103://加速
Skill ("get_103");
break;
case 104://隐身
yield return new WaitForSeconds (1);
break;
case 105://定身
if (aiType == AIType.敌人)
playerobj.SendMessage ("Skill", "get_105");
break;
case 201://无敌
if (aiType == AIType.玩家 && transform.position.y > w_down.position.y) {
Skill ("get_201");
}
break;
case 202://气球
Skill ("get_202");
//yield return new WaitForSeconds (1);
break;
case 203://减速
if (aiType == AIType.玩家) {
Debug.Log (aiType);
//enemy.Move_Speed *= 0.5f;
enemys = GameObject.FindGameObjectsWithTag ("enemybody");
foreach (GameObject obj in enemys) {
obj.SendMessage ("Skill", "get_203");
Debug.Log ("messagesend");
}
//yield return new WaitForSeconds (5.0f);
//enemy.Move_Speed = old_speed;
}
//yield return new WaitForSeconds (1);
break;
case 204://加速
if (aiType == AIType.玩家) {
Skill ("get_204");
}
break;
case 303://加速
if (aiType == AIType.玩家) {
StartCoroutine (player.FenShen ());
}
break;
}
yield return new WaitForSeconds (0.1f);
}
|
|