377节附近开始的2D游戏实例,基础的功能开发可以参考。
碰撞忽略
private void Start()
{
// 让第八层和第九层忽略碰撞,此段代码可以写在通用脚本内
Physics2D.IgnoreLayerCollision(8, 9);
}
Easy Touch插件
可以用来做摇杆,功能简单,不多写了。
插件中有一些关于event的实例脚本(JoystickEnent、EasyJoystick等),可以用来参考。
触摸事件
可以看看API中的Input类、Touch类,多点触控可以看一下GetTouch方法。
TouchPhase触摸相位(枚举),通过这个枚举来判断触摸的状态:
枚举值 | 描述 |
---|---|
Began | 手指触摸了屏幕。 |
Moved | 手指在屏幕上进行了移动。 |
Stationary | 手指正在触摸屏幕但尚未移动。 |
Ended | 从屏幕上抬起了手指,只是最后一个触摸阶段。 |
Canceled | 系统取消了对触摸的跟踪,如用户把屏幕放到脸上或者touch点过多时,这是一个触摸的最后状态。 |
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TouchPhaseExample : MonoBehaviour
{
public Vector2 startPos;
public Vector2 direction;
public Text m_Text;
string message;
void Update()
{
//Update the Text on the screen depending on current TouchPhase, and the current direction vector
m_Text.text = "Touch : " + message + "in direction" + direction;
// Track a single touch as a direction control.
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
// Handle finger movements based on TouchPhase
switch (touch.phase)
{
//When a touch has first been detected, change the message and record the starting position
case TouchPhase.Began:
// Record initial touch position.
startPos = touch.position;
message = "Begun ";
break;
//Determine if the touch is a moving touch
case TouchPhase.Moved:
// Determine direction by comparing the current touch position with the initial one
direction = touch.position - startPos;
message = "Moving ";
break;
case TouchPhase.Ended:
// Report that the touch has ended when it ends
message = "Ending ";
break;
}
}
}
}
Unity Remote远程调试
手机装APP,PC端装安卓环境。
AR(AugmentedReality)增强现实
常用插件:
- Vuforia 高通,不支持Mac PC
- EasyAR 国内开发的AR插件,使用方便,移动设备、PC都可以
- ARToolKit 开发难度较大,使用人少
- Metaio 被苹果收购
EasyAR插件
需要用公司信息(Bundle ID或Package Name)到官网去注册,获取Key以后才可以使用。
读取文本文件
TextAsset.text此方式只能一次性读取文件中的所有内容。
读取远程文件
public class TestWWW : MonoBehaviour
{
public string url = "http://www.xxx.com/xxx/xxx.jpg";
IEnumerator start()
{
WWW www = new WWW(URL);
yield return www;
renderer.material.mainTexture = www.texture;
}
}
更多内容请查看总目录【Unity】Unity学习笔记目录整理