- Home /
Question by
UnityGuy1243 · Nov 15, 2011 at 10:02 PM ·
javascriptconvert
Convert To UnityScript
How would I convert the following XNA script to UnityScript:
private Texture2D CreateStaticMap(int resolution)
{
Random rand = new Random();
Color[] noisyColors = new Color[resolution * resolution];
for (int x = 0; x < resolution; x++)
for (int y = 0; y < resolution; y++)
noisyColors[x + y * resolution] = new Color(new Vector3((float)rand.Next(1000) / 1000.0f, 0, 0));
Texture2D noiseImage = new Texture2D(device, resolution, resolution, 1, TextureUsage.None, SurfaceFormat.Color);
noiseImage.SetData(noisyColors);
return noiseImage;
}
I tried but got a handful of errors:
var resolution : int;
function Start(){
CreateStaticMap();
}
function CreateStaticMap(){
var noisyColors : Color[] = new Color[resolution * resolution];
for(x=0; x<=resolution; x++){
for(y=0; y<=resolution; y++){
noisyColors[x + y * resolution] = new Color(Vector3(System.Random.Next(1000) / 1000.0, 0, 0));
}
}
var noiseImage : Texture2D = new Texture2D( //I have no idea what to do now
}
Comment
Best Answer
Answer by aldonaletto · Nov 15, 2011 at 10:27 PM
The conversion to JS is something like below. The Texture2D() in noiseImage declaration seems to have parameters in excess.
function CreateStaticMap(resolution: int): Texture2D { var rand = new Random(); var noisyColors: Color[] = new Color[resolution * resolution]; for (var x = 0; x < resolution; x++) for (var y = 0; y < resolution; y++) noisyColors[x + y * resolution] = Color(rand.Next(1000)/1000.0, 0, 0); // this Texture2D constructor seems to have too many arguments: var noiseImage: Texture2D = Texture2D(device, resolution, resolution, 1, TextureUsage.None, SurfaceFormat.Color); noiseImage.SetData(noisyColors); return noiseImage; }
Your answer
![](https://koobas.hobune.stream/wayback/20220613043848im_/https://answers.unity.com/themes/thub/images/avi.jpg)