- Home /
Question by
shads11858 · Oct 30, 2013 at 03:29 AM ·
editorgridprojectprojector
Projector working in Editor, but disappearing after Build & Run
This is a bit of strange one I think and not sure why it's not working.. I created a projector using scripts for a terrain grid sqr so it would follow the mouse and highlight the grid tile that a ray hits. Works great when I run the game using play button inside Unity editor, but as soon as I build and run the project as exe, the projector isn't there anymore and not sure why. I'll copy in some of the code I've got for it.
private float brushSize = 4.0f;
private Texture2D brushes;
private int brushSizePaint = 3;
void Start(){
brushes = Resources.LoadAssetAtPath("Assets/PATileTerrain/Brushes/patileterrain_brush1.png", typeof(Texture2D)) as Texture2D;
InitPreview();
}
public void InitPreview(){
GameObject previewObject = new GameObject("PATileTerrainPreview");
previewObject.hideFlags = HideFlags.HideInHierarchy;
previewProjector = previewObject.AddComponent<Projector>();
previewProjector.nearClipPlane = terrain.settings.minHeight - 1.0f;
previewProjector.farClipPlane = terrain.settings.maxHeight + 1.0f;
previewProjector.orthographic = true;
previewProjector.orthographicSize = brushSize;
previewProjector.transform.Rotate(-90.0f, 0.0f, 0.0f);
Shader previewShader = Shader.Find("Hidden/PATileTerrainPreview");
Material previewMaterial = new Material(previewShader);
previewMaterial.SetColor("_Color", new Color(0.32f, 0.36f, 0.6f, 0.07f));
previewMaterial.SetTexture("_Brush", brushes);
previewProjector.material = previewMaterial;
previewProjector.enabled = true;
/*[1.04]*/ previewProjector.transform.parent = terrain.transform;
}
public void UpdatePreview()
{
if (previewProjector)
{
previewProjector.nearClipPlane = terrain.settings.minHeight - 1.0f;
previewProjector.farClipPlane = terrain.settings.maxHeight + 1.0f;
previewProjector.material.SetColor("_Color", new Color(0.32f, 0.36f, 0.6f, 0.97f));
previewProjector.material.SetTexture("_Brush", brushes);
if (show3x3){
previewProjector.orthographicSize = (terrain.settings.tileSize * 0.6f)* brushSizePaint;
}else previewProjector.orthographicSize = terrain.settings.tileSize * 0.6f;
}
}
void Update ()
{
// ** THIS SECTION GETS POINTS using raycasts & x,y for tile seleciton **
RaycastHit hit;
Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit, Mathf.Infinity);
int x, y;
Vector3 pos;
pos = terrain.transform.InverseTransformPoint(hit.point);
x = (int)Mathf.Abs(pos.x / terrain.tileSize);
y = (int)Mathf.Abs(pos.z / terrain.tileSize);
// ** THIS SECTION GETS POINTS **
//uses the previewProjector created object to show highlighted grid sqr.
PATileTerrain.PATile selectedTile = terrain.GetTile(x,y); //called every update so tile position is used in IF statement to update mousemovement
//as long as tile and projector are assigned values, show highlighted projector on map.
if (selectedTile != null && previewProjector != null){
previewProjector.transform.position = terrain.transform.TransformPoint(selectedTile.position);
previewProjector.transform.rotation = terrain.transform.localRotation;
previewProjector.transform.Rotate(90.0f, 0.0f, 0.0f);
}//end if on projector.
}
Comment