Other.
Unity包路径:C:\Users\Administrator.SKY-20170215WLI\AppData\Roaming\Unity\AssetStore-5.x
copy/b *.tdl 22.mp4(腾讯sql转换成mp4格式)
一. 用于暂停所有内容的方法
//EditorApplication.isPaused = true;(头文件
using UnityEditor;)与//Time.timeScale =0;不同.
二. 用于暂停Animator的方法
ani.speed = 0; 恢复 ani.speed = 1;
三. 播放视频的方法
publicMovieTexture StartMovie;
voidStart()
{
StartMovie.Play();
}
void OnGUI()
{
//绘制电影纹理
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), StartMovie, ScaleMode.StretchToFill); (全屏播放)
}
//暂停视频播放StartMovie.Stop();StartMovie.isPlaying(判断视频是否播放结束);
四.视频声音的播放
audio = gameObject.GetComponent<AudioSource>();
audio.clip = _mt_MovieT.audioClip;
audio.Play();
(给要播放视频的gameObject添加一个空的AudioSource)
五.视频格式转换不装QuickTime插件
六.自动寻路
1.创建地形
2.添加角色
3.创建多个障碍物,尽量摆的复杂一点,来检查Navmesh的可用性和效率。
4.选中地形,在Navigation窗口中,设置Navigation Static
5.依次选中障碍物,在avigation窗口中,设置Navigation Static
7.Navigation窗口中,选择Bake(烘焙)界面,点击Bake按钮,进程场景烘焙,就可以烘焙出寻路网格了
8.为角色添加NavMeshAgent组件。Component->Navigation->Nav Mesh Agent
9.为角色新增一个脚本PlayerController.cs,实现点击目标,自动寻路功能
using UnityEngine;
using System.Collections;
public class ddd : MonoBehaviour
{
private NavMeshAgent agent;
void Start()
{
//获取组件
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
//鼠标左键点击
if (Input.GetMouseButtonDown(0))
{
//摄像机到点击位置的的射线
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//判断点击的是否地形
if (!hit.collider.name.Equals("Terrain"))
{
return;
}
//点击位置坐标
Vector3 point = hit.point;
//转向
transform.LookAt(new Vector3(point.x,transform.position.y, point.z));
//设置寻路的目标点
agent.SetDestination(point);
}
}
//播放动画,判断是否到达了目的地,播放空闲或者跑步动画
//if (agent.remainingDistance == 0)
//{
// animation.Play("idle");
//}
//else
//{
// animation.Play("run");
//}
}
}
七.只渲染摄像机能看见的一部分(减少内存开销)
1.将需要渲染的游戏物体在Static设置为OccluderStatic和Occludee Static
2.在Windows中找到OcclusionCulling在Occlusion Areas中create一个OcclusionArea放大包围整个场景)
3.点击Bake(烘焙)
八.在unity3d中让某个物体透明,以一个最简单的立方体为例,说明怎么样使物体透明。
菜单:GameObject->Create Other->Cube
这样就在场景中创建了一个立方体。
随便导入一张图片。
然后选中他,就可以在右侧的inspector窗口中看到它的所有属性,将刚才导入的图片直接从Hierarchy视图中拖拽到立方体的属性窗口的空白处。这时候你就会发现Material下面多出一个Shader的东西,这个东西有一项是texture,它里面就是我们刚才拖进去的图片,在看看立方体上面已经是被我们的图片所覆盖了。
接下来就是真正让立方体透明的东西了。
在shader右侧的下拉框中,选择transparant->Diffuse。完了后点击maincolor的右侧的白色矩形框(这里是设置立方体的颜色),弹出的框里面,有设置red, green, blue, Aphla, 分别用它们的首字母替代的,我们调整A的值,默认是255, 就是完全不透明,0是完全透明。接下来就根据你的需要自己调整吧。
使用代码修改的话: gameObject.renderer.material.color.a = 0.4;
九.如何调整粒子特效的大小
在此处调整粒子特效的大小
十.VR
①Unity3d中需要设置的
在打包PC模式下的PlaySetting中设置AutoGraphics API for Windows(去掉对勾),去掉对勾后会出现下面两个Direct3D9和Direct3D11去掉Direct3D11(有些电脑不支持)
再在下面有一个Virtual RealitySupported勾选上(虚拟现实支持)
②安装Unity支持包(OculusUnityIntegration.unitypackage),默认安装即可。
安装完毕之后Assets下面会有一个OVR的文件夹,找到该文件夹下的预制体Prefabs,第一个为Oculus摄像机(VR摄像机),第二个为VR的第一人称控制器,拖到需要的地方使用即可。
OVRShimLoader该脚本中的PlayerSettings.d3d11ForceExclusiveMode =false; // TODO: Re-enable when DX11 exclusive mode issue in 4.5.5 is fixed会报错
将该行注释掉即可。
③安装Oculus的SDK(oculus_runtime_rev_1_sdk_0.4.3_win)。(需要重启)
④有关电脑上的设置
在显示器屏幕分辨率中会检查到VR设备的显示器(眼镜中的显示器),选择该显示器,设置方向为纵向,确定即可。
将要看的内容脱出显示器最右边,拖出去(拖出去的部分即为显示在眼镜中的内容),像投影仪一样。
十一.服务器(Network)
using UnityEngine;
using System.Collections;
public class MyNwtwork : MonoBehaviour
{
public int connections = 10;//最多可以连接十个客户端
public int listenPort = 8899;//端口
public bool useNat = false;//不适用NAT技术
private string ip = "192.168.0.150";
public GameObject _go_player;
void OnGUI()
{
if (Network.peerType == NetworkPeerType.Disconnected)//如果服务器没有创建完成
{
if (GUILayout.Button("创建服务器"))
{
//进行服务器的操作
NetworkConnectionError erro = Network.InitializeServer(connections, listenPort, useNat);
print(erro);
}
if (GUILayout.Button("连接服务器"))
{
NetworkConnectionError erro = Network.Connect(ip, listenPort);
print(erro);
}
}
else if (Network.peerType == NetworkPeerType.Server)//如果服务器创建完成
{
GUILayout.Label("服务器创建完成");
}
else if (Network.peerType == NetworkPeerType.Client)
{
GUILayout.Label("客户端已经接入");
}
}
//这两个方法都是在服务器端调用的
void OnServerInitialized()//OnServerInitialized当服务器初始化完成后被调用
{
print("服务器初始化完成");
int group = int.Parse(Network.player + "");
Network.Instantiate(_go_player, new Vector3(0, 10, 0), Quaternion.identity, group);
}
void OnPlayerConnected(NetworkPlayer player)
{
print("一个客户端连接过来,Index Number:" + player);
}
//这个函数是在客户端被调用的
void OnConnectedToServer()
{
int group = int.Parse(Network.player + "");
Network.Instantiate(_go_player, new Vector3(0, 10, 0), Quaternion.identity, group);
print("成功连接到服务器");
}
//Network View组件用来在局域网之内同步一个游戏物体组件属性
//NetWork View会把创建出来它的客户端作为主人(比如:在一号exe上创建了服务器,
//二号exe作为客户端,此时界面上出现两个Cube,一个Cube的主人是一号,另外一个Cube
//的主人是二号,要修改第一个Cube的位置旋转等信息的时候必须通过一号exe修改画面才
//会同步,如果通过二号exe修改第一个Cube的位置旋转等信息,则画面不同步)
}
注意:(客户端加入服务器输入的IP必须是你现在这台电脑上的IP地址不能是其他地址,否则该客户端无法加入服务器)
解决局域网卡顿的方法(增加网络更新的默认发送速率)
void Awake()
{
Network.sendRate = 100;
}
十二.对材质球进行剃光处理(取出黑色的边缘,做子弹闪光时的Quad的材质)
十三.在局域网的创建中使用RPC(远程过程调用)
十四.如何利用代码获取本机的IP地址
// Use this for initialization
void Start()
{
GetAddressIP();
}
// Update is called once per frame
void Update()
{
}
void GetAddressIP()
{
///获取本地的IP地址
string AddressIP = string.Empty;
foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
{
if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
{
AddressIP = _IPAddress.ToString();
}
}
print(AddressIP);
}
十五.改变单个动画的播放速度
AnimationState state = _a_Animation["Fire"];
state.speed = 3F;
十六. public void OnMouseUp()和public void OnPointerUp(PointerEventData eventData)的区别
1.public void OnMouseUp():在点击游戏物体(3D模型)的时候被调用
2.public voidOnPointerUp(PointerEventData eventData):在点击UI图片的时候被调用。如果在场景中的摄像机上加上PhysicsRaycaster组件,则public void OnPointerUp(PointerEventDataeventData)函数也可以在3D游戏物体上被调用。
3.使用public void OnPointerUp(PointerEventData eventData)该函数的时候注意应当与public void OnPointerDown(PointerEventData eventData)一起使用,少了Down使用单独使用Up没有任何作用,但是单独使用Down是有效的。
4.当3D物体已经在一个BoxCollider中的时候无法调用OnMouseDown函数
十七.如何得到3D物体的Size?
以size的x方向为例
1:gameObject.renderer.bounds.size.x;//这个值的结果真实反应出有MeshRenderer这个组件的模型的尺寸。不需要再乘以localScale.x。
2:gameObject.GetComponent<MeshFilter>().mesh.bounds.size.x;//通过MeshFilter获得原始模型的mesh,该值返回的结果是原始mesh的尺寸。
若要获得模型的尺寸大小还需要乘以模型的localScale.x。
即:gameObject.GetComponent<MeshFilter>().mesh.bounds.size.x*gameObject.transform.localScale.x;
3:为物体添加Collider,然后使用XXX.collider.bounds.size;
这个不一定能很好的反应物体的大小,bounds获得的是物体的外包矩形。而且这个外包矩形的X,Y,Z和世界坐标一致。因此,若物体有旋转,获得的尺寸就不能反应出物体的真实大小,只是其外包矩形的大小。。。
如:获得terrain的尺寸
terrainWidth = terrain.collider.bounds.size.x;
terrainLength = terrain.collider.bounds.size.z;
terrainHeight = terrain.collider.bounds.size.y;
4:代码实现获得复杂物体的尺寸(诸如根节点没有MeshFilter,MeshRenderer组件,物体是由很多复杂不规则的小mesh子物体组成的)
如:
Camera的口径Size
当投影类型为Perspective时,fieldOfView属性表示口径的度数,范围为[1,179]
当投影类型为Orthgraphic, orthographicSize属性为正交模式下的口径尺寸
十八. 如何拖拽地图
using UnityEngine;
usingSystem.Collections;
public class DragMap: MonoBehaviour
{
public float speed;//注意填写Speed
bool isDown = false;
Vector3 offset;
Vector3 lastPos;
Vector3 nowPos;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(2) &&!isDown)
{
isDown = true;
lastPos = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(2))
isDown = false;
if (isDown)
{
nowPos = Input.mousePosition;
offset = nowPos - lastPos;
offset *= speed * Time.deltaTime;
Vector3 camPos =Camera.main.transform.position;
Camera.main.transform.position =new Vector3(camPos.x - offset.x, camPos.y, camPos.z - offset.y);
lastPos = nowPos;
}
}
}
十九.用代码控制全屏非全屏
void Update()
{
//退出全屏
if (Input.GetKeyDown(KeyCode.Escape))
Screen.fullScreen = false;
//全屏
if (Input.GetKey(KeyCode.A))
{
//获取设置当前屏幕分辩率
Resolution[] resolutions =Screen.resolutions;
//设置当前分辨率
Screen.SetResolution(resolutions[resolutions.Length - 1].width,resolutions[resolutions.Length - 1].height, true);//true表示是全屏,false表示非全屏
Screen.fullScreen = true; //设置成全屏,
}
}
二十.从Asset文件夹中动态加载一个图片到场景中去
using UnityEngine;
using System.Collections;
using UnityEditor;
public class Test : MonoBehaviour
{
//贴图
Texture2D texture;
// Use this forinitialization
void Start()
{
//从project视图中记载资源
texture = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/0.jpg",typeof(Texture2D));
}
void OnGUI()
{
//渲染资源
GUI.DrawTexture(new Rect(0, 0, texture.width, texture.height),texture);
}
// Update iscalled once per frame
void Update()
{
}
}
二十一.当摄像机拉高之后模型没有阴影,怎么显示出来
Edit->ProjectSettings->Quality->ShadowsDistance(调大该值即可)。
二十二.调用CMD
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using System.Threading;
public class RunCMD : MonoBehaviour
{
public static string global_cmd = "calc";
void Start()
{
//Thread newThread = new Thread(newThreadStart(NewThread));
//newThread.Start();
//for (int i = 0; i < 4; i++)
RunCmd(global_cmd);
}
void NewThread()
{
for (int i = 0; i < 4; i++)
RunCmd(global_cmd);
}
// Update is called once per frame
void Update()
{
}
private void RunCmd(string cmd)
{
//例Process
print("dddd");
Process p = new Process();
p.StartInfo.FileName = "cmd.exe"; //确定程序名
p.StartInfo.Arguments = "/c " + cmd; //确定程式命令行
p.StartInfo.UseShellExecute = false; //Shell的使用
p.StartInfo.RedirectStandardInput = false; //重定向输入
p.StartInfo.RedirectStandardOutput = false; //重定向输出
p.StartInfo.RedirectStandardError = false; //重定向输出错误
p.StartInfo.CreateNoWindow = false; //设置置不显示示窗口
p.Start();
//returnp.StandardOutput.ReadToEnd(); //输出出流取得命令行结果果
}
}
二十三. 3D声音的调节
调节Spatial Blend为3D音乐(值为1),Reverb Zone Mix为1.1,Doppler Level为1,Spread为0,Volume Rolloff为LinearRolloff,Max Distance为30
二十四. Unity编辑器扩展
①:[Serializable]//将下面这个结构体中的对象统一在一起用一个组的形式(using System)
public class AnimNames
{
public string OpeningAnim = "Door_open";
public string LockedAnim = "Door_locked";
}
②:[Tooltip("player'shead with collider in trigger mode. Type your tag here (usually it is MainCamera)")]//鼠标放到这个Player Head Tag上的时候显示下面的提示
public string PlayerHeadTag = "MainCamera";
③:[HideInInspector]//在Inspector面板中隐藏一个公有变量