只贴代码主要是给自己看的## 标题
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class TestResources : MonoBehaviour
{
int index;
[SerializeField] Texture[] Tex;
public GameObject cube;//作为预制体必须动态加载
GameObject clonegb;
public GameObject gb;
[SerializeField] AudioClip clip;
public AudioSource ads;
public Slider sl;
//[SerializeField]Material mat;
void Start()
{
//动态加载一个音频片段
//clip = Resources.Load(“Audio/朴树 - 平凡之路”);
//ads.clip = clip;//audiosource组件绑定的音频剪辑片段属性赋值
//ads.Play();//播放音乐
//Tex = Resources.LoadAll(“TechBlue”);
//gb.GetComponent().material.mainTexture = Tex[0];
动态加载一个材质球==================================
//mat = Resources.Load<Material>("Materials/CapsualMat");
//gb.GetComponent<MeshRenderer>().material = mat;
动态加载一个游戏对象================================
//cube = Resources.Load<GameObject>("Prefab/cube");
克隆出来
//clonegb = Instantiate(cube);
换贴图
//Tex = Resources.Load<Texture>("TechBlue/OUYA_O 24px");
//clonegb.GetComponent<MeshRenderer>().material.mainTexture = Tex;
//获取一张贴图资源:资源放在Resuorce文件夹下面,泛型格式传入参数,string类型的路径
//Tex = Resources.Load<Texture>("TechBlue/OUYA_O 24px");
//cube.GetComponent<MeshRenderer>().material.mainTexture = Tex;
ResourcesLoadAsyncClip();//异步加载音频的方法
}
ResourceRequest request;//异步读取的返回值
void ResourcesLoadAsyncClip()
{
request = Resources.LoadAsync<AudioClip>("Audio/朴树 - 平凡之路");//异步加载
}
void Update()
{
//实时的异步加载的当前进度,和slider进度条进行绑定
if(request != null)
{
float current_loadingprogress = request.progress;//获取到当前加载的进度
//slider赋值(0-1)
sl.value = current_loadingprogress;
sl.transform.Find("Handle Slide Area/Handle/Text").GetComponent<Text>().text = (current_loadingprogress * 100 + "%").ToString();
if (request.isDone)
{
ads.clip = request.asset as AudioClip;
ads.Play();
request = null; //加载完成,置空。释放空间
}
}
//if (Input.GetKeyDown(KeyCode.A))
//{
// gb.GetComponent<MeshRenderer>().material.mainTexture = Tex[random(index)];
//}
}
//int random(int a)
//{
// while(a == index)
// {
// a = Random.Range(0, Tex.Length);
// }
// index = a;
// return index;
//}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Security.Cryptography;public class TestSceneManager : MonoBehaviour
{
public Slider sl;
AsyncOperation sceneAsync;
AsyncOperation unloadAsync;//异步卸载某一个场景,返回值类型
// Start is called before the first frame update
void Start()
{
///
/// 同步加载场景
//方式一:传入进来的是场景的index
//SceneManager.LoadScene(0);
//方式二:传入进来的是场景的名字
//SceneManager.LoadScene(“test2”, LoadSceneMode.Single);//默认是single 加载单个场景
//SceneManager.LoadScene(“test2”,LoadSceneMode.Additive);//两个场景共存
///
///
/// 异步加载场景
///
sl.value = 0;
sceneAsync = SceneManager.LoadSceneAsync(“test2”, LoadSceneMode.Additive);
sceneAsync.completed += OnSceneLoadCompelete;//委托,事件绑定,+=后面写方法名
}
void Update()
{
if(sceneAsync != null)
{
Debug.Log(sceneAsync.progress);//当前加载的进度
sl.value = sceneAsync.progress;//赋值
}
sl.transform.Find(“Handle Slide Area/Handle/Text”).GetComponent
().text = (sl.value * 100 + “%”).ToString();
//按下空格键,卸载某一个场景
if (Input.GetKeyDown(KeyCode.Space))
{
unloadAsync = SceneManager.UnloadSceneAsync(“test2”);
//也可以绑定一个事件
unloadAsync.completed += OnSceneUnloadCompelete;
}
}
void OnSceneUnloadCompelete(AsyncOperation asop)
{
//可以写卸载某一场景后的效果
Debug.Log(“场景卸载完成”);
}
GameObject gb;
public GameObject[] objs;
void OnSceneLoadCompelete(AsyncOperation asop)
{
sl.value = asop.progress;//1
//Debug.Log(asop);
//Debug.Log(asop.progress);
Debug.Log("场景加载完成");
sceneAsync = null;
print(SceneManager.GetActiveScene().name);//Scene1
Scene scene1 = SceneManager.GetActiveScene();
Scene scene2 = SceneManager.GetSceneByName("test2");
Debug.Log(scene2.name);
gb = GameObject.Find("Capsule");
//获取某一个场景中的所有的位于根节点的对象
//objs = scene2.GetRootGameObjects();
//获取某一个场景中的所有的位于根节点的对象
objs = scene1.GetRootGameObjects();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class TestInput : MonoBehaviour
{
void Start()
{
}
bool isDoubleClick = false;
bool ismove = false;
bool isRot = false;
float rotateSpeed = 1;
float beginTime;//点击开始的时间
float endTime;//点击结束的时间
bool clickDouble = false;//是否双击
bool isScale = false;
//原来第一次的触摸状态
Touch oldTouch1;
Touch oldTouch2;
void Update()
{
Touch touch = Input.GetTouch(0);
if (Input.touchCount == 1)//如果是单手指触摸
{
if(ismove && touch.phase == TouchPhase.Moved)//单手指移动
{
Vector3 touchPos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 0));
//位置:赋值给Player
transform.position = new Vector3(touchPos.x, touchPos.y, transform.position.z);
}
if (isRot && touch.phase == TouchPhase.Moved)//单手指旋转
{
Vector2 deltaPos = touch.deltaPosition;//本次触摸距离上一次触摸的位置的改变量
if (deltaPos.x > 0 && deltaPos.magnitude > 1)//向右滑动
{
//旋转
transform.Rotate(0, -deltaPos.magnitude * rotateSpeed, 0);
}
if (deltaPos.x < 0 && deltaPos.magnitude > 1)//向左滑动
{
//旋转
transform.Rotate(0, deltaPos.magnitude * rotateSpeed, 0);
}
}
if(isDoubleClick && touch.phase == TouchPhase.Began)//点击开始
{
beginTime = Time.time;
float coldTime = beginTime - endTime;
if(coldTime > 0 && coldTime < 1)//第二次开始点击的时间 减去第一次结束点击的时间,求出的时间差在一秒范围内,模拟双击
{
clickDouble = true;
}
}
if(isDoubleClick && touch.phase == TouchPhase.Ended)//点击结束
{
endTime = Time.time;
if (clickDouble)//双击两次结束
{
endTime = 0;
clickDouble = false;
//双击的时候让模型归位
InitFunction();
}
}
}
if(isScale && Input.touchCount == 2)//双手指触摸实现缩放
{
Touch touch1 = Input.GetTouch(0);
Touch touch2 = Input.GetTouch(1);
//第二次触摸的开始状态,不做任何处理(不做缩放的效果)
if (touch1.phase == TouchPhase.Began)
{
oldTouch1 = touch;
oldTouch2 = touch2;
return;
}
//根据位置的改变量实现缩放
float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);//第二个手指刚刚放到触摸屏上的时候,两个手指之间的距离
float newDistance = Vector2.Distance(touch1.position, touch2.position);//两个手指的实时距离
float offset = newDistance - oldDistance;//求出2次的距离的增量
float scaleFactor = offset / 2000;//求出缩放系数
Camera.main.orthographicSize += offset;//改变摄像机的正交尺寸的范围
//限定摄像机视野范围的改变量
Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize,0.6f,8);
}
}
public void InitFunction()//模型回到最初位置
{
transform.position = new Vector3(0, 0, 0);
transform.rotation = Quaternion.Euler(0, 180, 0);
Camera.main.orthographicSize = 1; //将摄像机的尺寸大小设置为1
}
private void OnGUI()
{
GUILayout.Space(100);
ismove = GUILayout.Toggle(ismove, "Move", GUILayout.Width(100), GUILayout.Height(30));
isRot = GUILayout.Toggle(isRot, "Rotate", GUILayout.Width(100), GUILayout.Height(30));
isDoubleClick = GUILayout.Toggle(isDoubleClick, "你今天双击了嘛",GUILayout.Width(200),GUILayout.Height(30));
isScale = GUILayout.Toggle(isScale, "Scale", GUILayout.Width(200), GUILayout.Height(50));
}
}