So my code is below. What it does is instantiate gameobjects on the y axis. So in editor when looking in otrhographic view from above, you use the numpad for instantiate different blocks to build a level. But when you dont use it, and want to work as usual, it conflict as i described first of.
thanks for pointing out namespace collision orb, i will look in to that. whileBreak no i dont use it on a gameobject. Only as described above. eses I dont really se how I can implement your solution, maby thats only if thats on a gamObject? But I will check in to the code to understand it more.
thanks for reply!
this script is not attached to any gameobject
using UnityEngine;
using UnityEditor;
using System;
[CustomEditor(typeof(GameObject))]
public class PlaceMesh : Editor
{
public Vector2[] grid2DArray = new Vector2[2];
public string lastCommand;
public int gridSize = 6;
public float xi = 7.0f;
GameObject meshFromAssetFolder;
void OnSceneGUI()
{
if(!PlaceMeshWindow.navMeshEditMode==true)
{
Event e = Event.current;
//Debug.Log(e.keyCode.ToString());
switch (e.type)
{
case EventType.KeyUp:
string buttonEvent = Event.current.keyCode.ToString();
if (buttonEvent != "None")
PlacePiece(buttonEvent);
break;
}
}
}
public void PlacePiece(string buttonEvent)
{
// Ray rayHit = Camera.current.ScreenPointToRay(Input.mousePosition);
// rayHit.origin = new Vector3(rayHit.origin.x, 0f, rayHit.origin.z);
Vector2 guiPosition;
guiPosition = Event.current.mousePosition;
Ray ray = HandleUtility.GUIPointToWorldRay(guiPosition);
//Debug.Log(ray.origin);
float XValueNormalised = (ray.origin.x % gridSize) / gridSize;
float ZValueNormalised = (ray.origin.z % gridSize) / gridSize;
//Debug.Log("XValueNormalised ->" + XValueNormalised + " and as abs" + Math.Abs(XValueNormalised));
//Debug.Log("ZValueNormalised ->" + ZValueNormalised + " and as abs" + Math.Abs(ZValueNormalised));
if (Math.Abs(XValueNormalised) < 0.15 || Math.Abs(XValueNormalised) > 0.85)
{
Debug.Log("Left-Right axis to close in between two tiles!");
return;
}
if (Math.Abs(ZValueNormalised) < 0.15 || Math.Abs(ZValueNormalised) > 0.85)
{
Debug.Log("Up-Down axis to close in between two tiles!");
return;
}
float XCordinateToPlace = 0f;
float ZCordinateToPlace = 0f;
if (ray.origin.x > 0)
{
//Debug.Log("x" + (ray.origin.x - ray.origin.x % gridSize + gridSize / 2));
XCordinateToPlace = (ray.origin.x - ray.origin.x % gridSize + gridSize / 2);
}
if (ray.origin.x < 0)
{
//Debug.Log("x" + (ray.origin.x - ray.origin.x % gridSize - gridSize / 2));
XCordinateToPlace = (ray.origin.x - ray.origin.x % gridSize - gridSize / 2);
}
if (ray.origin.z > 0)
{
//Debug.Log("z" + (ray.origin.z - ray.origin.z % gridSize + gridSize / 2));
ZCordinateToPlace = (ray.origin.z - ray.origin.z % gridSize + gridSize / 2);
}
if (ray.origin.z < 0)
{
//Debug.Log("z" + (ray.origin.z - ray.origin.z % gridSize - gridSize / 2));
ZCordinateToPlace = (ray.origin.z - ray.origin.z % gridSize - gridSize / 2);
}
grid2DArray[0] = grid2DArray[1];
Vector3 spawnPos = new Vector3(XCordinateToPlace, 0, ZCordinateToPlace);
grid2DArray[1] = new Vector2(XCordinateToPlace,ZCordinateToPlace);
//Debug.Log(string.Format("spawnPosition {0}", spawnPos));
//Debug.Log(string.Format("Position 1 :{0} and Position 2 :{1}",grid2DArray[0],grid2DArray[1]));
TileToolObjects scriptTileToolObjects = GameObject.Find("ScriptHolder").GetComponent<TileToolObjects>();
switch (buttonEvent)
{
case "Keypad1":
meshFromAssetFolder = scriptTileToolObjects.NumPadButton1;
//meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube1.Prefab", typeof(GameObject));
//Debug.Log("Keypad1");
break;
case "Keypad2":
meshFromAssetFolder = scriptTileToolObjects.NumPadButton2;
//meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube2.Prefab", typeof(GameObject));
//Debug.Log("Keypad2");
break;
case "Keypad3":
meshFromAssetFolder = scriptTileToolObjects.NumPadButton3;
//meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube3.Prefab", typeof(GameObject));
//Debug.Log("Keypad3");
break;
case "Keypad4":
meshFromAssetFolder = scriptTileToolObjects.NumPadButton4;
//meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube3.Prefab", typeof(GameObject));
//Debug.Log("Keypad4");
break;
case "Keypad5":
meshFromAssetFolder = scriptTileToolObjects.NumPadButton5;
//meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube3.Prefab", typeof(GameObject));
//Debug.Log("Keypad5");
break;
case "Keypad6":
meshFromAssetFolder = scriptTileToolObjects.NumPadButton6;
//meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube3.Prefab", typeof(GameObject));
//Debug.Log("Keypad6");
break;
case "Keypad7":
meshFromAssetFolder = scriptTileToolObjects.NumPadButton7;
//meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube3.Prefab", typeof(GameObject));
//Debug.Log("Keypad7");
break;
case "Keypad8":
meshFromAssetFolder = scriptTileToolObjects.NumPadButton8;
//meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube3.Prefab", typeof(GameObject));
//Debug.Log("Keypad8");
break;
case "Keypad9":
meshFromAssetFolder = scriptTileToolObjects.NumPadButton9;
//meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube3.Prefab", typeof(GameObject));
//Debug.Log("Keypad9");
break;
case "Keypad0":
meshFromAssetFolder = null;
//meshFromAssetFolder = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Cube3.Prefab", typeof(GameObject));
//Debug.Log("Keypad9");
break;
case "Keypad5-ButtonNotAccecable":
Vector2[] matrix2x2 = new Vector2[2];
if (grid2DArray[0].y < grid2DArray[1].y)
{
matrix2x2[0] = grid2DArray[0];
matrix2x2[1] = grid2DArray[1];
}
else
{
matrix2x2[0] = grid2DArray[1];
matrix2x2[1] = grid2DArray[0];
}
if (grid2DArray[0].x < grid2DArray[1].x)
{
matrix2x2[0] = grid2DArray[0];
matrix2x2[1] = grid2DArray[1];
}
else
{
matrix2x2[0] = grid2DArray[1];
matrix2x2[1] = grid2DArray[0];
}
int xaxis=0;
int yaxis=0;
int plusx = 0;
int plusy = 0;
Debug.Log("from x:" + (3 - Math.Abs((int)matrix2x2[0].x)*gridSize));
Debug.Log("matrix x:" + (xaxis - Math.Abs((int)matrix2x2[1].x)));
Debug.Log("from y:" + (yaxis - Math.Abs((int)matrix2x2[1].y)));
Debug.Log("matrix y:" + (3 - Math.Abs((int)matrix2x2[0].y) * gridSize));
for (xaxis = 0-Math.Abs((int)matrix2x2[0].x); xaxis < (int)matrix2x2[1].x; xaxis+=6)
{
Debug.Log("inne i x");
for (yaxis = 0 - Math.Abs((int)matrix2x2[0].y); yaxis < (int)matrix2x2[1].y; yaxis+=6)
{
Debug.Log("inne i y");
Vector3 matrixSpawnPos = new Vector3(matrix2x2[0].x+plusx, 0f, matrix2x2[0].y+plusy);
Debug.Log(matrixSpawnPos);
float radiusInMatrix = 1f;
Collider[] hitCollidersInMatrix = Physics.OverlapSphere(matrixSpawnPos, radiusInMatrix);
int iInMatrix = 0;
while (iInMatrix < hitCollidersInMatrix.Length)
{
Debug.Log("hitCollider location " + matrixSpawnPos);
Debug.Log("hitColliders :" + hitCollidersInMatrix[iInMatrix]);
UnityEngine.Object.DestroyImmediate(hitCollidersInMatrix[iInMatrix].transform.gameObject);
iInMatrix++;
}
Instantiate(meshFromAssetFolder, matrixSpawnPos, Quaternion.identity);
plusy =+ gridSize;
}
plusx = +gridSize;
plusy = 0;
}
Debug.Log("Spawnvector from"+matrix2x2[0] + " to " + matrix2x2[1]);
return;
default:
Debug.Log("keycode not assigned :"+buttonEvent);
return;
}
float radius = 1f;
Collider[] hitColliders = Physics.OverlapSphere(spawnPos, radius);
int i = 0;
while (i < hitColliders.Length)
{
Debug.Log("hitColliders :" + hitColliders);
UnityEngine.Object.DestroyImmediate(hitColliders.transform.gameObject);
i++;
}
//Debug.Log("meshFromAssetFolder -->"+ meshFromAssetFolder);
//Instantiate(meshFromAssetFolder, spawnPos, Quaternion.identity);
//Instantiate(Resources.Load(meshFromAssetFolder.name), spawnPos,meshFromAssetFolder.transform.rotation);
Debug.Log(meshFromAssetFolder.name);
//GameObject go = Instantiate(Resources.Load(meshFromAssetFolder.name)) as GameObject;
GameObject go = PrefabUtility.InstantiatePrefab(meshFromAssetFolder) as GameObject;
go.transform.position = spawnPos;
lastCommand = buttonEvent;
}
}
There is also a gameobject in scene that “ScriptHolder” that holds this script.
*_ <em>*using System.Collections; using System.Collections.Generic; using UnityEngine; public class TileToolObjects : MonoBehaviour { public GameObject NumPadButton1; public GameObject NumPadButton2; public GameObject NumPadButton3; public GameObject NumPadButton4; public GameObject NumPadButton5; public GameObject NumPadButton6; public GameObject NumPadButton7; public GameObject NumPadButton8; public GameObject NumPadButton9; }*</em> _*