So I am trying to make 2 different texture2d's from 1. basically by cutting it in half. I tried a simple way, but it is kinda slow and right now ddoesn't work at all.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MediaPipe.HandPose
{
public sealed class camerasplitter : MonoBehaviour
{
public WebcamInput main, first, second;
public Texture2D camdata,firsttex,secondtex;
public int frame = 0;
// Start is called before the first frame update
void Start()
{
firsttex = new Texture2D(1080, 960);
secondtex = new Texture2D(1080, 960);
first._dummyImage = firsttex;
second._dummyImage = secondtex;
}
// Update is called once per frame
void Update()
{
frame++;
if (frame > 1000) frame = 1;
if (frame % 3 == 0)
{
camdata =toTexture2D(main.Texture);
for (int i = 0; i < camdata.height; i++)
{
for (int j = 0; j < camdata.width/2; j++)
{
firsttex.SetPixel(i, j, camdata.GetPixel(i, j));
secondtex.SetPixel(i, j + camdata.width / 2, camdata.GetPixel(i, j + camdata.width / 2));
}
}
firsttex.Apply(false);
/*for (int i = 0; i < camdata.height; i++)
{
for (int j = camdata.width / 2; j < camdata.width; j++)
{
secondtex.SetPixel(i, j, camdata.GetPixel(i, j));
}
}*/
secondtex.Apply(false);
}
}
public Texture2D toTexture2D(Texture rTex)
{
Texture2D dest = new Texture2D(rTex.width, rTex.height, TextureFormat.RGBA32, false);
Graphics.CopyTexture(rTex, dest);
return dest;
}
}
}
when i run it, the camdata is getting data from other script and it shows. but firsttex and secondtex are not showing. they are gray. even if it lags to do the "for" operation.
thats the thing that is stopping my project. Thanks in advance!
Related
So, my editor keeps on crashing on running these scripts.
Specifications of my pc(if needed): Intel i5,8GB ram,Windows 10
I am trying to make a Minecraft game and my editor crashes:
VoxelData.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class VoxelData
{
public static readonly int ChunkWidth = 2;
public static readonly int ChunkHeight = 2;
public static readonly Vector3[] voxelVerts = new Vector3[8] {
new Vector3(0.0f,0.0f,0.0f),
new Vector3(1.0f,0.0f,0.0f),
new Vector3(1.0f,1.0f,0.0f),
new Vector3(0.0f,1.0f,0.0f),
new Vector3(0.0f,0.0f,1.0f),
new Vector3(1.0f,0.0f,1.0f),
new Vector3(1.0f,1.0f,1.0f),
new Vector3(0.0f,1.0f,1.0f),
};
public static readonly int[,] voxelTris = new int[6, 6]{
{0,3,1,1,3,2},
{5,6,4,4,6,7},
{3,7,2,2,7,6}, // top face
{1,5,0,0,5,4},
{4,7,0,0,7,3},
{1,2,5,5,2,6}
};
}
and the Chunk.cs script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chunk : MonoBehaviour
{
public MeshRenderer meshRenderer;
public MeshFilter meshFilter;
// Start is called before the first frame update
int vertexIndex = 0;
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
List<Vector2> uvs = new List<Vector2>();
void Start()
{
for(int y = 0; y < VoxelData.ChunkHeight; y++)
{
for (int x = 0; x < VoxelData.ChunkWidth; x++)
{
for (int z = 0; x < VoxelData.ChunkWidth; z++)
{
AddVoxelDataToChunk(new Vector3(x, y, z));
}
}
}
CreateMesh();
}
void AddVoxelDataToChunk(Vector3 pos)
{
for (int p = 0; p < 6; p++)
{
for (int i = 0; i < 6; i++)
{
int triangleIndex = VoxelData.voxelTris[p, i];
vertices.Add(VoxelData.voxelVerts[triangleIndex] + pos);
triangles.Add(vertexIndex);
uvs.Add(Vector2.zero);
vertexIndex++;
}
}
}
void CreateMesh()
{
Mesh mesh = new Mesh();
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.uv = uvs.ToArray();
mesh.RecalculateNormals();
meshFilter.mesh = mesh;
}
// Update is called once per frame
}
why does it keep crashing, it is quite annoying and not letting me continue on this project.
Please help and advise what to do
The most inner
// | here !
// v
for (int z = 0; x < VoxelData.ChunkWidth; z++)
{
AddVoxelDataToChunk(new Vector3(x, y, z));
}
will cause an infinite loop since x is nowhere changed inside it.
It should most probably be z
// | here !
// v
for (int z = 0; z < VoxelData.ChunkWidth; z++)
{
AddVoxelDataToChunk(new Vector3(x, y, z));
}
This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 1 year ago.
IndexOutOfRangeException: Index was outside the bounds of the array. (wrapper managed-to-managed) System.Object.ElementAddr_4(object,int,int,int) Camera_s.Start () (at Assets/scripts/Camera_s.cs:19)
Script Move.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
public Block scr;
public Camera_s cam;
public Transform my_block;
private int[,,] grid;
void Start()
{
my_block = gameObject.transform.parent;
scr = my_block.GetComponent<Block>();
cam = GameObject.Find("Main Camera").GetComponent<Camera_s>();
grid = cam.Grid;
}
void OnMouseDown(){
if (scr.IsActive){
if (gameObject.transform.name == "left"){
my_block.position -= new Vector3(cam.Jump_Size, 0f);
}
else if (gameObject.transform.name == "right"){
my_block.position += new Vector3(cam.Jump_Size, 0f);
}
else if (gameObject.transform.name == "up"){
my_block.position += new Vector3(0f, cam.Jump_Size);
}
else if (gameObject.transform.name == "down"){
my_block.position -= new Vector3(0f, cam.Jump_Size);
}
}
}
void Update()
{
}
}
Script Block.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Block : MonoBehaviour
{
public bool IsActive = false;
public Camera_s Camera_Script;
public int Id;
public int[] coords;
public int color;
private int[,,] grid;
void Start()
{
Camera_Script = GameObject.Find("Main Camera").GetComponent<Camera_s>();
Id = Camera_Script.Block_max + 1;
Camera_Script.Block_max += 1;
grid = Camera_Script.Grid;
grid[coords[0], coords[1], 0] = 1;
grid[coords[0], coords[1], 0] = color;
}
void OnMouseDown(){
IsActive = true;
Camera_Script.Active = Id;
Debug.Log("Active");
}
// Update is called once per frame
void Update()
{
if (Camera_Script.Active != Id){
IsActive = false;
}
}
}
Script Camera_s.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera_s : MonoBehaviour
{
public int Active = -1;
public int Block_max = -1;
public float Jump_Size;
public int[] Grid_Size;
public int[,,] Grid;
void Start()
{
Grid = new int[Grid_Size[1], Grid_Size[0], 2];
for (int i = 0;i < 3;i++){
for (int j = 0;j< 5;j++){
Grid[j, i, 0] = 0;
Grid[j, i, 1] = 0;
}
}
}
// Update is called once per frame
void Update()
{
}
}
I understand something wrong with my for, but i dont know how to fix it.
Main part of code, in which I had mistake:
for (int i = 0;i < 3;i++){
for (int j = 0;i < 5;j++){
Grid[j, i, 0] = 0;
Grid[j, i, 1] = 0;
}
}
Data inputed by Unity Editor:
Jump_Size = 3f; Grid_Size = [5, 3]
Thanks!
If what you input is really Grid_Size = [5, 3], that means your first two dimensions are flipped.
You could simply correct that, but you should not expose the sizes if anything but one value pair is generating errors.
Grid = new int[Grid_Size[0], Grid_Size[1], 2];
for (int i = 0;i < Grid.GetLength(1);i++){
for (int j = 0;j< Grid.GetLength(0);j++){
Grid[j, i, 0] = 0;
Grid[j, i, 1] = 0;
}
}
Would result in the same array dimensions as before if you flip the input, but still use the same access order as before. How exactly you do this depends on what you want to do with the array of course.
Additionally it would be advisable to check the input for correct values, in Block you are indexing Grid with input (coords) from the editor again. I'd recommend to make sure that input is in expected bounds of the array
I'm following along a course on udemy which is for free it's a memory game (https://www.udemy.com/xamarin-native-ios-memory-game-csharp/learn/v4/overview)
Now with the randomizer method i get a new problem with the same out of range error
using System;
using System.Collections;
using System.Collections.Generic;
using UIKit;
namespace iOSMemoryGame
{
public partial class ViewController : UIViewController
{
#region vars
List<String> imgArr = new List<String>();
float gameViewWidth;
int gridSize = 6;
ArrayList tilesArr = new ArrayList();
ArrayList coordsArr = new ArrayList();
#endregion
public ViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
// make sure Game View is Laid Out
gameView.LayoutIfNeeded();
gameViewWidth = (float)gameView.Frame.Size.Width;
// let's load all of our images into an array
for (int i = 1; i <= 18; i++)
{
imgArr.Add("img_" + i.ToString() + ".png");
}
// let's make a call to tileMaker
tileMaker();
// let's call the randomizer
randomizer();
}
private void randomizer()
{
// we are gonna go through our tiles in ORDER
// and we are gonna assign a new center to them RANDOMLY
foreach (UIView any in tilesArr)
{
// UIImageView thisTile = (UIImageView)tilesArr[i];
Random myRand = new Random();
int randomIndex = myRand.Next(0, coordsArr.Count);
CoreGraphics.CGPoint newRandCenter = (CoreGraphics.CGPoint)coordsArr[randomIndex];
any.Center = newRandCenter;
coordsArr.RemoveAt(randomIndex);
}
}
private void tileMaker()
{
float tileWidth = gameViewWidth / gridSize;
float xCenter = tileWidth / 2;
float yCenter = tileWidth / 2;
int imgCounter = 0;
for (int h = 0; h < gridSize; h++)
{
for (int v = 0; v < gridSize; v++)
{
UIImageView tileImgView = new UIImageView();
CoreGraphics.CGPoint newCenter = new CoreGraphics.CGPoint(xCenter, yCenter);
tileImgView.Frame = new CoreGraphics.CGRect(0, 0, tileWidth - 5, tileWidth - 5);
String imgName = imgArr[imgCounter];
tileImgView.Image = new UIImage(imgName);
tileImgView.Center = newCenter;
// user CAN interact with this image view
tileImgView.UserInteractionEnabled = true;
// adding the new image view to the array
tilesArr.Add(tileImgView);
gameView.AddSubview(tileImgView);
xCenter = xCenter + tileWidth;
imgCounter++;
if (imgCounter == gridSize * gridSize / 2)
{
imgCounter = 0;
}
}
xCenter = tileWidth / 2;
yCenter = yCenter + tileWidth;
}
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
partial void Rst4Button_TouchUpInside(UIButton sender)
{
throw new NotImplementedException();
}
partial void Rst6Button_TouchUpInside(UIButton sender)
{
throw new NotImplementedException();
}
}
}
Something is wrong with the randomizer method but i dont know what.
it gives me again a out of range error.
without the randomizer method it works fine
This line
for (int i = 1; i < 18; i++)
creates 17 images, not 18 as expected by the following loops.
// 0-5 = 6 loops
for (int h = 0; h < gridSize; h++)
{
// 0-5 = 6 loops (h*v=18)
for (int v = 0; v < gridSize; v++)
{
....
You need to write
for (int i = 0; i < 18; i++)
imgArr.Add("img_" + (i+1).ToString() + ".png");
I'm making a tiled strategy game alhoug it doesn't even resemble that in complexity. however, I ran into something i can't figure out myself. it should make the tile a click red and if i click it again it should make it normal again. but what it does is: in the first 4 or so tiles it makes everythinng red on the opposide sideflipped verticly,, but that should be fixable by reversing and x and y value. however after the fourth tile everything just get's placed on a random place(or as it seems atleast).i really can't figure it out myself and i have rewritten it from scratch 3 times already. I would be really thankfull if a more experienced (or less experienced is also okay) programmer could point me to my problem
My code(chopped up in chunks of releveance ):
protected override void LoadContent()
{
baseLayer.LoadContent("TextFile1.txt");
standardTexture = Content.Load<Texture2D>("Tiles/Grass/GrassTile1");
selectRects = new Rectangle[(int)baseLayer.MapDimensions.X, (int)baseLayer.MapDimensions.Y];
while (j<baseLayer.MapDimensions.Y)
{
while (i < baseLayer.MapDimensions.X)
{
selectRects[i, j] = new Rectangle(i * standardTexture.Width, j * standardTexture.Height,
standardTexture.Width, standardTexture.Height);
i++;
}
i = 0;
j++;
}
j = 0;
}
protected override void Update(GameTime gameTime)
{
while(j<baseLayer.MapDimensions.Y)
{
while(i<baseLayer.MapDimensions.X)
{
if (standardCheck.Click(selectRects[i, j], mouseState, prevMouseState))
{
if (colorArray[i, j] != Color.White)
colorArray[i, j] = Color.White;
else
colorArray[i, j] = Color.Red;
}
i++;
}
i = 0;
j++;
}
j = 0;
and my main drawing class(a litle bigger):
public class LoadMap
{
{
get { return map; }
set { map = value; }
}
public Vector2 MapDimensions
{
get { return dimensions; }
set { dimensions = value; }
}
public LoadMap()
{
lineindex = 0;
}
public void Draw(SpriteBatch spriteBatch, Texture2D[] maptexture,Color[,] color)
{
for (int i = 0; i < dimensions.X; i++)
{
for (int j = 0; j < dimensions.Y; j++)
{
if (map[i, j] == 1)
{
spriteBatch.Draw(maptexture[0],
new Vector2(j * maptexture[0].Width, i * maptexture[0].Height), color[i,j]);
}
if (map[i, j] == 2)
{
spriteBatch.Draw(maptexture[1],
new Vector2(j * maptexture[0].Width, i * maptexture[0].Height), color[i, j]);
}
if (map[i, j] == 3)
{
spriteBatch.Draw(maptexture[2],
new Vector2(j * maptexture[0].Width, i * maptexture[0].Height), color[i, j]);
}
}
I have been following the Unity3D Procedural Cave Generation, but I found an error very early on in MapGeneration.cs. Unity says that on line 1 word 1, there is an error: Identifier expected: 'public' is a keyword. I cannot see any difference from my code and the tutorial's code. Here is the link to the tutorial video: [\Tutorial video 1] and here is my code:
using UnityEngine;
using System.Collections;
using System
public class MapGeneration : MonoBehaviour {
public int width;
public int height;
public string seed;
public bool useRandomSeed;
[Range(0,100)]
public int randomFillPercent;
int[,] map;
void Start() {
GenerateMap();
}
void GenerateMap() {
map = new int[width,height];
}
void RandomFillMap() {
if (useRandomSeed) {
seed = Time.time.ToString();
}
System.Random psuedoRandom = new System.Random(seed.GetHashCode());
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y ++) {
map[x,y] = (psuedoRandom.Next(0,100) < randomFillPercent)? 1: 0;
}
}
}
void OnDrawGizmos() {
if (map != null) {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y ++) {
Gizmos.color = (map[x,y] == 1)? Color.black: Color.white;
Vector3 position = new Vector3(-width/2 + x + .5f,0,-height/2 + y + .5f);
Gizmos.DrawCube(position,Vector3.one);
}
}
}
}
}
The error is public on line one.
You don't have the ; after using System (that, maybe, is also an incomplete import).