unity3d添加腳本
1. unity3d 相機怎麼添加腳本
由於項目需求,需要在unity中播放高清視頻,視頻解析度達到了3840x1200。採用的是c++
plugin解碼視頻,提供圖片紋理給unity渲染的方式。而在unity中使用的是rendertexture來保存解碼的視頻圖片。為了方面調試,需要保存某一些時刻的圖片數據到本地,可以採用下面的函數實現:
[csharp]
view
plain
[contextmenu("save
png")]
private
void
savetexturetofile()
{
if
(outputtexture
!=
null)
{
rendertexture
prev
=
rendertexture.active;
rendertexture.active
=
target;
texture2d
png
=
new
texture2d(outputtexture.width,
outputtexture.height,
textureformat.argb32,
false);
png.readpixels(new
rect(0,
0,
outputtexture.width,
outputtexture.height),
0,
0);
byte[]
bytes
=
png.encodetopng();
string
path
=
string.format("mp/raw
{0}.png",
random.range(0,
65536).tostring("x"));
filestream
file
=
file.open(path,
filemode.create);
binarywriter
writer
=
new
binarywriter(file);
writer.write(bytes);
file.close();
texture2d.destroy(png);
png
=
null;
rendertexture.active
=
prev;
}
}
2. Unity3D中三種調用其他腳本函數的方法
第一種:被調用腳本函數為static類型,調用時直接用 腳本名.函數名()。很不實用……
第二種:GameObject.Find("腳本所在物體名").SendMessage("函數名"); 此種方法可以調用public和private類型函數
第三種:GameObject.Find("腳本所在物體名").GetComponent<腳本名>().函數名();此種方法只可以調用public類型函數
3. unity3d怎麼給組件添加腳本
組件就是靠添加腳本實現的,你說的是給對象添加腳本吧。點擊inspector下面有個AddComponet.
4. unity3d中如何用腳本創建對象或者類
你要創建什麼對象?如果是unity3d中的物體那是可以的,如果是腳本,不好意思,沒見過動態創建腳本的,因為unity3d與其他引擎最大的不同在於它的gameobject和腳本使用方式,unity3d中腳本生效是通過掛載在物體上實現的。
只能動態的將寫好的腳本添加到物體上,無法動態的新建腳本
//給游戲物體添加名為FoobarScript的腳本
var fbs : FoobarScript;
fbs = gameObject.AddComponent(FoobarScript);
這是js寫法
public FoobarScript fbs;
public void Awake() {
fbs = gameObject.AddComponent();
}
這是C#寫法
5. Unity3d如何寫一個用可視化的按鈕控制物體的顯示和隱藏的腳本
1.新腔羨建工程,創建C#腳本,名稱自擬。
程序清單:
using UnityEngine;
using System.Collections;
public class Button1 : MonoBehaviour {
// 標志符,用於控制敬洞按鈕文本
public int flag = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
// GUI函數
void OnGUI () {
// 隱藏按鈕
if (flag == 1) {
if (GUI.Button (new Rect (100, 100, 100, 100), "隱藏")) {
flag ++;
flag %= 2;
}
}
// 顯示按鈕
else {
if (GUI.Button (new Rect (100, 100, 100, 100), "顯示")) {
flag ++;
flag %= 2;
}
}
// 顯示物體,但不影響按鈕
if (flag == 1) {
transform.renderer.enabled = true;
}
// 隱藏物體,但不影響按鈕
else {
transform.renderer.enabled = false;
}
}
}
6. unity3d中如何給多個預制體添加同一腳本
2018把prefab加了嵌套,你看到教程估計是舊版本教程;
你可以把所有預設拖到場景中,批量添加組件後,再批量apply(override)