VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > C#编程 >
  • C#教程之Unity实现鼠标或者手指点击模型播放动画

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

本文实例为大家分享了unity鼠标或者手指点击模型播放动的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using UnityEngine;
using System.Collections;
 
public class ClickPlayAnimation : MonoBehaviour {
  /// <summary>
  /// 实现功能为点击模型播放动画
  /// 使用方法,给模型添加碰撞,添加脚本
  /// </summary>
  
  bool isPlayAnim = false;
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
    //animation.Play();
    if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android)
    {
       foreach (Touch touch in Input.touches)
      {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(0).phase == TouchPhase.Moved)
        {
          Ray ray = Camera.main.ScreenPointToRay(new Vector3(touch.position.x, touch.position.y, 0));
          RaycastHit hit;
 
          if (Physics.Raycast(ray, out hit))
          {
            // Debug.DrawLine(ray.origin, hit.point);
            if (hit.collider.gameObject.name == gameObject.name)
            {
              isPlayAnim = true;
              print("123");
            }
          }
        }
      }
    }
    else
    {
      if (Input.GetMouseButtonDown(0))
      {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
 
        if (Physics.Raycast(ray, out hit))
        {
          Debug.DrawLine(ray.origin, hit.point);
          //print(hit.collider.gameObject.name);
 
          //curObject = hit.collider.gameObject;
          if (hit.collider.gameObject.name == gameObject.name)
          {
            isPlayAnim = true;
            print("123");
          }
          // 显示当前选中对象的名称
         // print(hit.collider.gameObject);
        }
 
      }
    }
    
    if(isPlayAnim)
    {
      animation.Play();
      isPlayAnim = false;
    }
 
 }
  
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关教程