- Home /
Insert the texture means the URL
Hello i want insert the texture on plane means the URL. I use this script in C# but the system don't error returns:
using UnityEngine;
using System.Collections;
using System.IO;
public class OpenFilePanel : MonoBehaviour {
bool modys = false;
bool modus = false;
private Rect windowRect = new Rect(Screen.width/2, 300,320,150);
string Text = "";
string NameT = "";
void OnMouseDown() {
modys = !modys;
}
void OnGUI () {
if(modys){
// Register the window. Notice the 3rd parameter
windowRect = GUI.Window (0, windowRect, DoMyWindow, "My Window");
}
}
// Make the contents of the window
private void DoMyWindow (int id) {
GUILayout.Label("URL:");
Text = GUILayout.TextField(Text, GUILayout.Width(300) , GUILayout.Height(20));
GUILayout.Label("NameTexture:");
NameT = GUILayout.TextField(NameT, GUILayout.Width(300) , GUILayout.Height(20));
GUILayout.BeginHorizontal();
if(GUILayout.Button ("Save")) {
modus = !modus;
Save();
}
if(GUILayout.Button ("Close")) {
modys = false;
}
GUILayout.EndHorizontal();
}
// Update is called once per frame
IEnumerator Save () {
if(modus){
string url = Text;
print(url);
// Create a texture in DXT1 format
renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false);
while (true) {
// Start a download of the given URL
var www = new WWW(url);
yield return www;
// assign the downloaded image to the main texture of the object
www.LoadImageIntoTexture((Texture2D)renderer.material.mainTexture);
}
}
}
void Start()
{
StartCoroutine("WritePNG");
}
IEnumerator WritePNG()
{
// We should only read the screen buffer after rendering is complete
yield return new WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0, false);
//tex.Apply(); //Needed? setPixels not called and it works without Apply.
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
Destroy(tex);
// Write to a file in the project folder
File.WriteAllBytes(Application.dataPath + "/../" + NameT + ".png", bytes);
}
}
Recapitulating:
1- I want to submit a URL to take the picture. 2 - I want to be able to immediately place the texture on the floor. 3 - I want to save the texture.
Thank you for your attention. Help me.
Comment