API自学记录(四)
程序员文章站
2024-03-02 15:55:22
...
API自学记录(四)
如何通过WWW下载图片
支持http:// https:// file:// ftp://(匿名下载)四种下载方式
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WWWTest : MonoBehaviour {
public string url = "一个图片的网址";
IEnumerator Start()
{
WWW www = new WWW(url);
yield return www;
Renderer renderer = GetComponent<Renderer>();
renderer.material.mainTexture = www.texture;
}
// Update is called once per frame
void Update () {
}
}
Touches触摸事件
GetTouch获得每个触摸事件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Touches : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Debug.Log(Input.touches.Length);
if (Input.touches.Length > 0)
{
Touch touch1 = Input.touches[0];
//touch1.position;//获取位置
TouchPhase phase = touch1.phase;//获取触摸状态如began/moved等
}
}
}
关于Camera组件的作用和一些属性介绍
Current获取到当前相机
Main获取当前场景main camera 通过标签获取,如多个只取第一个
ScreenPointToRay检测碰撞
使用Camera把屏幕坐标转换成射线:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraTest : MonoBehaviour {
private Camera camera;
// Use this for initialization
void Start () {
camera = Camera.main;
}
// Update is called once per frame
void Update () {
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
//Debug.DrawRay(ray.origin, ray.direction);
//Debug.DrawLine(ray.origin, ray.origin + ray.direction * 100);
RaycastHit hit;
bool isCollider = Physics.Raycast(ray, out hit);
Debug.Log(hit.collider);
}
}
角色控制器CharacterController的使用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_CC : MonoBehaviour {
private CharacterController cc;
public float speed = 3;
// Use this for initialization
void Start () {
cc = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
cc.SimpleMove(new Vector3(h, 0, v) * speed);//会模拟重力
//cc.Move(new Vector3(h, 0, v) * speed * Time.deltaTime);
//由直接给定的速度移动
Debug.Log(cc.isGrounded);
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
Debug.Log(hit.collider);//输出碰到的物体
}
}
Mesh的设置
Mesh是一个网格,控制模型的外观
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeshAndMat : MonoBehaviour {
public Mesh mesh;
// Use this for initialization
void Start () {
GetComponent<MeshFilter>().sharedMesh = mesh;//速度快
Debug.Log(GetComponent<MeshFilter>().mesh == mesh);
}
}
Material材质类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeshAndMat : MonoBehaviour {
private Material mat;
void Start () {
mat = GetComponent<MeshRenderer>().material;
}
// Update is called once per frame
void Update () {
mat.color = Color.Lerp(mat.color, Color.red, Time.deltaTime);
}
}
Unity API方法变更
unity 4.x 5.x 2017
GetComponent() 代替 rigidbody2D
GetComponent() 代替 rigidbody
GetComponent() 代替 audio
Unity 5.3:
ParticleSystem main = smokePuff.GetComponent();
main.startColor
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnityAPIChange : MonoBehaviour {
private Rigidbody rgd;
// Use this for initialization
void Start () {
rgd = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
//rigidbody.AddForce(Vector3.one);
//rgd.AddForce(Vector3.one);
//audio.Play();//弃用的
//GetComponent<AudioSource>().Play();
//GetComponent<Rigidbody2D>();
//GetComponent<MeshRenderer>();
}
}
Unity API方法变更2-粒子系统变更
Unity 5.5+:
ParticleSystem.MainModule main = smokePuff.GetComponent().main;
main.startColor
Unity API方法变更3-场景切换相关API变更
SceneManagement 代替 Application
OnLevelWasLoaded() 在 Unity 5中被弃用了。
2D/3D sound 如何设置2D/3D声音
//Application.LoadLevel("Level2");//过时的
SceneManager.LoadScene("Scene2");//加载场景2
Scene scene = SceneManager.GetActiveScene();//正在运行的场景
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);//重新加载
上一篇: executors
下一篇: React setState 引发的思考