第一个问题:如何获得unity中的控件以及如何通过控件获得绑定的脚本文件
listener.Play();
}
1,获得
Camera:
Camera
mainCamera
= (
Camera
)
transform
.
GetComponent
(
"
Camera
"
);
2,获得text:
(
Text
)
GameObject
.
Find
(
"
PosText
"
).
transform
.
GetComponent
(
"
Text
"
);
3,获得音乐控制:
AudioSource
audioMgr
= (
AudioSource
)
transform
.
GetComponent
(
"
AudioSource
"
);
4,获得
helloworld
helloCSharp
=
mainCamera
.
GetComponent
<</span>helloworld> ();//获得组建绑定的脚本文件
5,停止脚本执行:h
elloCSharp
.
enabled
=
false
;
6,获得脚本中的API;
helloworld.
LaunchBullet();变量也是如此,通过“.”的方式
7,获得刚体:
Rigidbody
rigidbody
= (
Rigidbody
)
GameObject.Find ("bullet")
.
GetComponent
(
"
Rigidbody
"
);
第二个问题:如何通过prefab克隆同样的控件
1,
Transform
bullet =
GameObject
.
Find
(
"
bullet
"
).transform
2,
Transform
obje
=
Instantiate
(
bullet
,
transform
.
position
,
transform
.
rotation
)
as
Transform
;
第三个问题:如何通过代码创建相关控件
1,创建UI相关控件要将创建方法写道
void
OnGUI
(),这个方法也是
called
once
per
frame
if
(
GUI
.
Button
(
new
Rect
(
startX
,
screenH
*
0.1f
,
w
,
h
),
"
Play
"
)){
Debug.Log("Play");listener.Play();
}
if (GUI.RepeatButton(new Rect(startX,screenH-height*3.3f,width,height),"LeftR")){
Vector3 rotation = new Vector3(0,this.speed*Time.deltaTime/2,0);
this.RotationObject(rotation,transform);
}
Vector3 rotation = new Vector3(0,this.speed*Time.deltaTime/2,0);
this.RotationObject(rotation,transform);
}
以上两种方法的创建不同之处就在于回调函数的调用,前者是在touchEnd的时候出发,后者是在按下的时 候持续触发
第四个问题:如何获得点击事件
1,获得pc端的事件:
if (Input.GetKey (KeyCode.Space)) {
Debug.Log("you pressed :Space ");
}
if (Input.GetKey (KeyCode.Q)) {
Debug.Log("you pressed Q");
}
Debug.Log("you pressed :Space ");
}
if (Input.GetKey (KeyCode.Q)) {
Debug.Log("you pressed Q");
}
if (Input.GetButtonDown ("Fire1")) {
Vector3 mousePosition = Input.mousePosition;
Debug.Log(mousePosition);
}
Vector3 mousePosition = Input.mousePosition;
Debug.Log(mousePosition);
}
2,手机端的点击事件:
if(Input.touchCount>0) {
Touch touch = Input.touches[0];
Vector2 touchPosition = touch.position;
//将屏幕坐标转换到游戏场景中
// Vector3 bulletPos = mainCamera.ScreenToWorldPoint(new Vector3(touchPosition.x,touchPosition.y,transform.position.z));
if(touch.phase == TouchPhase.Began) {
}
if(touch.phase == TouchPhase.Moved) {
}
if(touch.phase == TouchPhase.Stationary) {
// this.LaunchBullet(bulletPos);
}
if(touch.phase == TouchPhase.Canceled) {
}
if(touch.phase == TouchPhase.Ended) {
PosText.text = touchPosition.x + "," + touchPosition.y;
}
}
Touch touch = Input.touches[0];
Vector2 touchPosition = touch.position;
//将屏幕坐标转换到游戏场景中
// Vector3 bulletPos = mainCamera.ScreenToWorldPoint(new Vector3(touchPosition.x,touchPosition.y,transform.position.z));
if(touch.phase == TouchPhase.Began) {
}
if(touch.phase == TouchPhase.Moved) {
}
if(touch.phase == TouchPhase.Stationary) {
// this.LaunchBullet(bulletPos);
}
if(touch.phase == TouchPhase.Canceled) {
}
if(touch.phase == TouchPhase.Ended) {
PosText.text = touchPosition.x + "," + touchPosition.y;
}
}
第五个问题:碰撞检测问题
碰撞检测两个API分别在开始碰撞和结束碰撞的时候出现,注意的是碰撞的两个物体都需要有刚体组件
void OnCollisionEnter(Collision collision){
// Debug.Log ("OnCollisionEnter"+collision.gameObject.name+isBase);
// Rigidbody rig = (Rigidbody)collision.transform.GetComponent ("Rigidbody");
if (! isBase ){
if (collision.gameObject.name !="bullet(Clone)"){
audioMgr.Play ();
Destroy(gameObject,0.75f);
}
}
}
void OnCollisionExit(Collision collision) {
print("No longer in contact with " + collision.transform.name);
}
// Debug.Log ("OnCollisionEnter"+collision.gameObject.name+isBase);
// Rigidbody rig = (Rigidbody)collision.transform.GetComponent ("Rigidbody");
if (! isBase ){
if (collision.gameObject.name !="bullet(Clone)"){
audioMgr.Play ();
Destroy(gameObject,0.75f);
}
}
}
void OnCollisionExit(Collision collision) {
print("No longer in contact with " + collision.transform.name);
}
第六个问题:有关android上动态字体显示不全
1,不要用unity默认的字体Arial,自己下载.ttf的字体导入到unity工程中替换默认字体,主要是因为默认字体包含的字基本都是英文的
第七个问题:如何开启和退出以及重新加载游戏
1,退出游戏:
Application
.
Quit
();
2,重新加载游戏:
Application
.
LoadLevel
(
"
helloWorld
"
);
3,打开网址:
Application
.
OpenURL
(
"
http://blog.sina.com.cn/u/3166357177
"
);
第八个问题:如何发布各个版本
1,iOS版本,File->Building Setting->Build
2,发布android
3,pc端
4,在发布版本的时候一定要把unity默认的的bundle Identifier 改掉,e.g:com.test.helloWorld,
5,在发布android的时候一定要更新相关sdk版本,通过eclipse由于国内网站比较慢,所以建议直接下载覆盖,推荐网站:
http://www.cnblogs.com/bjzhanghao/archive/2012/11/14/android-platform-sdk-download-mirror.html