1  /**
2  * UniMove API - A Unity plugin
for the PlayStation Move motion controller
3  * Copyright (C)
2012, 2013, Copenhagen Game Collective (http://www.cphgc.org)
4  * Patrick Jarnfelt
5  * Douglas Wilson (http://www.doougle.net)
6  *
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use
in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  *
1. Redistributions of source code must retain the above copyright
14  * notice,
this list of conditions and the following disclaimer.
15  *
16  *
2. Redistributions in binary form must reproduce the above copyright
17  * notice,
this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  **/

32
33 using
UnityEngine;
34 using
System;
35 using
System.Collections.Generic;
36
37 public
class UniMoveTest : MonoBehaviour
38 {
39     
// We save a list of Move controllers.
40     List<UniMoveController> moves =
new List<UniMoveController>();
41
42     
void Start()
43     {
44         
/* NOTE! We recommend that you limit the maximum frequency between frames.
45          * This
is because the controllers use Update() and not FixedUpdate(),
46          * and yet need to update often enough to respond sufficiently fast.
47          * Unity advises to keep
this value "between 1/10th and 1/3th of a second."
48          * However, even
100 milliseconds could seem slightly sluggish, so you
49          * might want to experiment w/ reducing
this value even more.
50          * Obviously,
this should only be relevant in case your framerare is starting
51          * to lag. Most of the time, Update() should be called very regularly.
52          */

53         Time.maximumDeltaTime =
0.1f;
54         
55         
int count = UniMoveController.GetNumConnected();
56         
57         
// Iterate through all connections (USB and Bluetooth)
58         
for (int i = 0; i < count; i++)
59         {
60             UniMoveController move = gameObject.AddComponent<UniMoveController>();
// It's a MonoBehaviour, so we can't just call a constructor
61             
62             
// Remember to initialize!
63             
if (!move.Init(i))
64             {
65                 Destroy(move);
// If it failed to initialize, destroy and continue on
66                 
continue;
67             }
68                     
69             
// This example program only uses Bluetooth-connected controllers
70             PSMoveConnectionType conn = move.ConnectionType;
71             
if (conn == PSMoveConnectionType.Unknown || conn == PSMoveConnectionType.USB)
72             {
73                 Destroy(move);
74             }
75             
else
76             {
77                 moves.Add(move);
78                 
79                 move.OnControllerDisconnected += HandleControllerDisconnected;
80                 
81                 
// Start all controllers with a white LED
82                 move.SetLED(Color.white);
83             }
84         }
85         
if (moves.Count<=0) Debug.Log ("No Bluetooth-connected controllers found. Make sure one or more are both paired and connected to this computer.");
86     }
87
88     
89     
void Update()
90     {
91         
92         
foreach (UniMoveController move in moves)
93         {
94             
// Instead of this somewhat kludge-y check, we'd probably want to remove/destroy
95             
// the now-defunct controller in the disconnected event handler below.
96             
if (move.Disconnected) continue;
97             
98             
// Button events. Works like Unity's Input.GetButton
99             
if (move.GetButtonDown(PSMoveButton.Circle)){
100                 Debug.Log(
"Circle Down");
101             }
102             
if (move.GetButtonUp(PSMoveButton.Circle)){
103                 Debug.Log(
"Circle UP");
104             }
105             
106             
// Change the colors of the LEDs based on which button has just been pressed:
107             
if (move.GetButtonDown(PSMoveButton.Circle)) move.SetLED(Color.cyan);
108             
else if(move.GetButtonDown(PSMoveButton.Cross)) move.SetLED(Color.red);
109             
else if(move.GetButtonDown(PSMoveButton.Square)) move.SetLED(Color.yellow);
110             
else if(move.GetButtonDown(PSMoveButton.Triangle)) move.SetLED(Color.green);
111             
else if(move.GetButtonDown(PSMoveButton.Move)) move.SetLED(Color.black);
112             
else if(move.GetButtonDown(PSMoveButton.Trigger)) move.SetLED(Color.blue);
113
114             
// Set the rumble based on how much the trigger is down
115             move.SetRumble(move.Trigger);
116         }
117     }
118     
119     
void HandleControllerDisconnected (object sender, EventArgs e)
120     {
121         
// TODO: Remove this disconnected controller from the list and maybe give an update to the player
122     }
123     
124     
void OnGUI()
125     {
126         
string display = "";
127         
128         
if (moves.Count > 0)
129         {
130             
for (int i = 0; i < moves.Count; i++)
131             {
132                 display +=
string.Format("PS Move {0}: ax:{1:0.000}, ay:{2:0.000}, az:{3:0.000} gx:{4:0.000}, gy:{5:0.000}, gz:{6:0.000}\n",
133                     i+
1, moves[i].Acceleration.x, moves[i].Acceleration.y, moves[i].Acceleration.z,
134                     moves[i].Gyro.x, moves[i].Gyro.y, moves[i].Gyro.z);
135             }
136         }
137
138         GUI.Label(
new Rect(10, Screen.height-100, 500, 100), display);
139     }
140 }


* Copyright (C) 2012, 2013, Copenhagen Game Collective (http:www.cphgc.org)

* Douglas Wilson (http:www.doougle.net)

We save a list of Move controllers.

Iterate through all connections (USB and Bluetooth)

UniMoveController move = gameObject.AddComponent(); It's a MonoBehaviour, so we can't just call a constructor

Remember to initialize!

Destroy(move); If it failed to initialize, destroy and continue on

This example program only uses Bluetooth-connected controllers

Start all controllers with a white LED

Instead of this somewhat kludge-y check, we'd probably want to removedestroy

the now-defunct controller in the disconnected event handler below.

Button events. Works like Unity's Input.GetButton

Change the colors of the LEDs based on which button has just been pressed:

Set the rumble based on how much the trigger is down

TODO: Remove this disconnected controller from the list and maybe give an update to the player




trò chơi game bóng rổ full code 31.714 lượt xem

Gõ tìm kiếm nhanh...