我需要将一个RenderTexture对象保存为.png文件,然后将该文件用作包裹3D对象的纹理.我的问题是现在我无法使用EncodeToPNG()保存RenderTexture对象,因为RenderTexture不包含该方法.如何将RenderTexture对象转换为Texture2D对象?谢谢!
// Saves texture as PNG file.
using UnityEngine;
using System.Collections;
using System.IO;
public class SaveTexture : MonoBehaviour {
public RenderTexture tex;
// Save Texture as PNG
void SaveTexturePNG()
{
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
Object.Destroy(tex);
// For testing purposes, also write to a file in the project folder
File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
}
}
Run Code Online (Sandbox Code Playgroud) 如何在网格布局中获取对象的实际位置?在我当前的迭代中,symbolPosition保持返回0,0,0.
public void OnPointerClick (PointerEventData eventData)
{
// Instantiate an object on click and parent to grid
symbolCharacter = Instantiate(Resources.Load ("Prefabs/Symbols/SymbolImage1")) as GameObject;
symbolCharacter.transform.SetParent(GameObject.FindGameObjectWithTag("MessagePanel").transform);
// Set scale of all objects added
symbolCharacter.transform.localScale = symbolScale;
// Find position of objects in grid
symbolPosition = symbolCharacter.transform.position;
Debug.Log(symbolPosition);
}
Run Code Online (Sandbox Code Playgroud) 当多个玩家连接到我的服务器时,我无法通过我的网络发送数据。我收到错误:
Trying to send command for object without authority.
UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String)
Network_Transmitter:CallCmdPrepareToReceiveBytes(Int32, Int32)
<DoSendBytes>c__Iterator0:MoveNext() (at Assets/Scripts/Networking/Network_Transmitter.cs:63)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
Player_Data:PrepareServerData() (at Assets/Scripts/Player/Player_Data.cs:81)
Player_Data:OnStartLocalPlayer() (at Assets/Scripts/Player/Player_Data.cs:44)
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
Run Code Online (Sandbox Code Playgroud)
错误发生在我的 Player_Controller 脚本CmdPrepareToReceiveBytes(transmissionId, data.Length);中的函数行中DoSendBytes。我将这个脚本与我的 Network_Transmitter 脚本一起附加到一个游戏对象,其中 NetworkIdentity 被分配为本地玩家权限。一旦在服务器上完全接收到这个字节数组,我就会根据发送的数据在服务器上实例化一个预制件(serverAvatar),如下所示:
public class Server_InstantiatePrefab : NetworkBehaviour
{
[Server]
public void InstantiatePlayerOnServer(PlayerObject playerObj)
{
GameObject go = Instantiate(Player_Controller.instance.serverAvatar, new Vector3(playerObj.tranX, playerObj.tranY, playerObj.tranZ), Quaternion.identity) as GameObject;
StartCoroutine(DoLoadRawTextureData(go, playerObj.texBytes, playerObj.texWidth, playerObj.texHeight, playerObj.texFormat));
}
[Server]
IEnumerator DoLoadRawTextureData(GameObject go, byte[] texBytes, int texWidth, int texHeight, TextureFormat texFormat)
{
Texture2D …Run Code Online (Sandbox Code Playgroud) 我在网页上设置了一个画布,其中运行着js动画,该动画填充了整个浏览器窗口,但是当我调整窗口的大小时,似乎无法自动调整画布的大小以适应新的窗口大小。有人可以告诉我我需要添加到我的js中才能正确获得此功能吗?谢谢!
var canvas = document.getElementById('canvas');
var makecodeplay = canvas.getContext('2d');
canvas.width=window.innerWidth;
canvas.height=window.innerHeight;
makecodeplay.clearRect(0, 0, canvas.width, canvas.height);
makecodeplay.fillStyle = "rgb(75,77,81)";
makecodeplay.fillRect(0, 0, canvas.width, canvas.height);
function randomPaint(inX, inY) {
var x = Math.floor(Math.random() * canvas.width);
var y = Math.floor(Math.random() * canvas.height);
var r, g, b;
r = Math.floor(Math.random() * 255);
g = Math.floor(Math.random() * 255);
b = Math.floor(Math.random() * 255);
makecodeplay.beginPath();
makecodeplay.fillStyle = "rgba(35,37,41,0.3)";
makecodeplay.fillRect(0, 0, canvas.width, canvas.height);
makecodeplay.fill();
makecodeplay.closePath();
makecodeplay.beginPath();
makecodeplay.strokeStyle = "rgba(" + r + "," + g + "," + …Run Code Online (Sandbox Code Playgroud)我正在尝试使用 unity 创建一个小测试应用程序。我正在构建应用程序的一个实例并将其设置为主机,然后在统一编辑器中作为客户端运行另一个实例。在当前的迭代中,我有一个按钮,应该在按下时截取屏幕截图,然后将该屏幕截图发送给所有客户端。但似乎没有任何效果。我遇到的最初问题是 ReadPixels 错误:
ReadPixels was called to read pixels from system frame buffer, while not inside drawing frame. UnityEngine.Texture2D:ReadPixels(Rect, Int32, Int32)
UNETChat:OnPostRender() (at Assets/Scripts/UNETChat.cs:50)
<TakeSnapshot>c__Iterator0:MoveNext() (at Assets/Scripts/UNETChat.cs:42)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr).
Beyond that, once solved I'm sure that there is a lot of additional work in here that's off the mark. But I'm just starting here. Thanks for the help!
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
public class MyMsgType
{
public static short texture = …Run Code Online (Sandbox Code Playgroud) 我在python脚本中遇到了这种语法,并看到它被称为切片赋值.你如何在C#中编写这种语法?
self.nav[:] = []
Run Code Online (Sandbox Code Playgroud) 用于构建文件列表的等效代码是什么?我正在创建这样的数组.
arctopithecusImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.jpg");
Run Code Online (Sandbox Code Playgroud)
我试着用它来构建我的List,但是它没有用.这样做的正确方法是什么?
List<FilePath> arctopithecusImageList = new List<FilePath>();
arctopithecusImageList.Add(Directory.GetFiles(@"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.jpg"));
Run Code Online (Sandbox Code Playgroud) 从switch语句返回GameObject的正确方法是什么?当我尝试返回头像时,我得到错误'使用未分配的局部变量'头像''.关于如何使用switch语句返回工作,我有点混淆了.谢谢您的帮助!
private GameObject GetAnimalAvatar(string animal)
{
GameObject avatar;
switch (animal)
{
case "bear":
avatar = ForestGameManager.fgm.bearAvatar;
break;
case "boar":
avatar = ForestGameManager.fgm.boarAvatar;
break;
case "doe":
avatar = ForestGameManager.fgm.doeAvatar;
break;
default:
break;
}
return avatar;
}
Run Code Online (Sandbox Code Playgroud) c# ×6
arrays ×1
client ×1
export ×1
grid-layout ×1
html5 ×1
io ×1
javascript ×1
list ×1
networking ×1
python ×1
return ×1
slice ×1
texture2d ×1
transform ×1