void OnSceneGUI() { tk2dCamera target = this.target as tk2dCamera; Handles.color = new Color32(255, 255, 255, 255); #if UNITY_5 DrawCameraBounds(target.GetComponent <Camera>().worldToCameraMatrix, target.Editor__GetFinalProjectionMatrix()); Handles.color = new Color32(55, 203, 105, 102); DrawCameraBounds(target.GetComponent <Camera>().worldToCameraMatrix, target.Editor__GetNativeProjectionMatrix()); #else DrawCameraBounds(target.camera.worldToCameraMatrix, target.Editor__GetFinalProjectionMatrix()); Handles.color = new Color32(55, 203, 105, 102); DrawCameraBounds(target.camera.worldToCameraMatrix, target.Editor__GetNativeProjectionMatrix()); #endif Handles.color = Color.white; // Draw scene gui if (sceneGUIHandler == null) { sceneGUIHandler = new tk2dEditor.tk2dCameraSceneGUI(); } sceneGUIHandler.OnSceneGUI(target); }
void Spawn() { //proper camera spawn on GUI, but couldnt get it to move Image spawnInstance; spawnInstance = Instantiate(spawnTest, currentCam.GetComponent <Camera>().WorldToScreenPoint(new Vector3(-28.5f, -8f, 0f)), Quaternion.identity); spawnInstance.transform.SetParent(currentCanvas.transform, false); spawnInstance.GetComponent <RectTransform>().position = Vector2.MoveTowards(spawnInstance.GetComponent <RectTransform>().position, new Vector2(spawnInstance.GetComponent <RectTransform>().position.x + 30, spawnInstance.GetComponent <RectTransform>().position.y), 10f * Time.deltaTime); }
void DrawToolsGUI() { EditorGUILayout.LabelField("Tools", EditorStyles.boldLabel); EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(true)); if (GUILayout.Button("Create Anchor", EditorStyles.miniButton, GUILayout.ExpandWidth(false))) { tk2dCamera cam = (tk2dCamera)target; GameObject go = new GameObject("Anchor"); go.transform.parent = cam.transform; tk2dCameraAnchor cameraAnchor = go.AddComponent <tk2dCameraAnchor>(); cameraAnchor.AnchorCamera = cam.GetComponent <Camera>(); tk2dCameraAnchorEditor.UpdateAnchorName(cameraAnchor); EditorGUIUtility.PingObject(go); } EditorGUILayout.EndHorizontal(); }
void DrawEditorGUI() { if (GUILayout.Button("Commit")) { // Select all children, EXCLUDING self Transform[] allTransforms = batcher.transform.GetComponentsInChildren <Transform>(); allTransforms = (from t in allTransforms where t != batcher.transform select t).ToArray(); // sort sprites, smaller to larger z if (batcher.CheckFlag(tk2dStaticSpriteBatcher.Flags.SortToCamera)) { tk2dCamera tk2dCam = tk2dCamera.CameraForLayer(batcher.gameObject.layer); Camera cam = tk2dCam ? tk2dCam.GetComponent <Camera>() : Camera.main; allTransforms = (from t in allTransforms orderby cam.WorldToScreenPoint((t.GetComponent <Renderer>() != null) ? t.GetComponent <Renderer>().bounds.center : t.position).z descending select t).ToArray(); } else { allTransforms = (from t in allTransforms orderby t.GetComponent <Renderer>().bounds.center.z descending select t).ToArray(); } // and within the z sort by material if (allTransforms.Length == 0) { EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: No child objects found", "Ok"); return; } Dictionary <Transform, int> batchedSpriteLookup = new Dictionary <Transform, int>(); batchedSpriteLookup[batcher.transform] = -1; Matrix4x4 batcherWorldToLocal = batcher.transform.worldToLocalMatrix; batcher.spriteCollection = null; batcher.batchedSprites = new tk2dBatchedSprite[allTransforms.Length]; List <tk2dTextMeshData> allTextMeshData = new List <tk2dTextMeshData>(); int currBatchedSprite = 0; foreach (var t in allTransforms) { tk2dBaseSprite baseSprite = t.GetComponent <tk2dBaseSprite>(); tk2dTextMesh textmesh = t.GetComponent <tk2dTextMesh>(); tk2dBatchedSprite bs = new tk2dBatchedSprite(); bs.name = t.gameObject.name; bs.position = t.localPosition; bs.rotation = t.localRotation; bs.relativeMatrix = batcherWorldToLocal * t.localToWorldMatrix; if (baseSprite) { bs.baseScale = Vector3.one; bs.localScale = new Vector3(t.localScale.x * baseSprite.scale.x, t.localScale.y * baseSprite.scale.y, t.localScale.z * baseSprite.scale.z); FillBatchedSprite(bs, t.gameObject); // temp redundant - just incase batcher expects to point to a valid one, somewhere we've missed batcher.spriteCollection = baseSprite.Collection; } else if (textmesh) { bs.spriteCollection = null; bs.type = tk2dBatchedSprite.Type.TextMesh; bs.color = textmesh.color; bs.baseScale = textmesh.scale; bs.renderLayer = textmesh.SortingOrder; bs.localScale = new Vector3(t.localScale.x * textmesh.scale.x, t.localScale.y * textmesh.scale.y, t.localScale.z * textmesh.scale.z); bs.FormattedText = textmesh.FormattedText; tk2dTextMeshData tmd = new tk2dTextMeshData(); tmd.font = textmesh.font; tmd.text = textmesh.text; tmd.color = textmesh.color; tmd.color2 = textmesh.color2; tmd.useGradient = textmesh.useGradient; tmd.textureGradient = textmesh.textureGradient; tmd.anchor = textmesh.anchor; tmd.kerning = textmesh.kerning; tmd.maxChars = textmesh.maxChars; tmd.inlineStyling = textmesh.inlineStyling; tmd.formatting = textmesh.formatting; tmd.wordWrapWidth = textmesh.wordWrapWidth; tmd.spacing = textmesh.Spacing; tmd.lineSpacing = textmesh.LineSpacing; bs.xRefId = allTextMeshData.Count; allTextMeshData.Add(tmd); } else { // Empty GameObject bs.spriteId = -1; bs.baseScale = Vector3.one; bs.localScale = t.localScale; bs.type = tk2dBatchedSprite.Type.EmptyGameObject; } batchedSpriteLookup[t] = currBatchedSprite; batcher.batchedSprites[currBatchedSprite++] = bs; } batcher.allTextMeshData = allTextMeshData.ToArray(); int idx = 0; foreach (var t in allTransforms) { var bs = batcher.batchedSprites[idx]; bs.parentId = batchedSpriteLookup[t.parent]; t.parent = batcher.transform; // unparent ++idx; } Transform[] directChildren = (from t in allTransforms where t.parent == batcher.transform select t).ToArray(); foreach (var t in directChildren) { GameObject.DestroyImmediate(t.gameObject); } Vector3 inverseScale = new Vector3(1.0f / batcher.scale.x, 1.0f / batcher.scale.y, 1.0f / batcher.scale.z); batcher.transform.localScale = Vector3.Scale(batcher.transform.localScale, inverseScale); batcher.Build(); EditorUtility.SetDirty(target); } }
void DrawCameraGUI(tk2dCamera target, bool complete) { bool allowProjectionParameters = target.SettingsRoot == target; bool oldGuiEnabled = GUI.enabled; SerializedObject so = this.serializedObject; #if UNITY_5 SerializedObject cam = new SerializedObject(target.GetComponent <Camera>()); #else SerializedObject cam = new SerializedObject(target.camera); #endif SerializedProperty m_ClearFlags = cam.FindProperty("m_ClearFlags"); SerializedProperty m_BackGroundColor = cam.FindProperty("m_BackGroundColor"); SerializedProperty m_CullingMask = cam.FindProperty("m_CullingMask"); SerializedProperty m_TargetTexture = cam.FindProperty("m_TargetTexture"); SerializedProperty m_Near = cam.FindProperty("near clip plane"); SerializedProperty m_Far = cam.FindProperty("far clip plane"); SerializedProperty m_Depth = cam.FindProperty("m_Depth"); SerializedProperty m_RenderingPath = cam.FindProperty("m_RenderingPath"); SerializedProperty m_HDR = cam.FindProperty("m_HDR"); if (complete) { EditorGUILayout.PropertyField(m_ClearFlags); EditorGUILayout.PropertyField(m_BackGroundColor); EditorGUILayout.PropertyField(m_CullingMask); EditorGUILayout.Space(); } tk2dCameraSettings cameraSettings = target.CameraSettings; tk2dCameraSettings inheritedSettings = target.SettingsRoot.CameraSettings; TransparencySortMode transparencySortMode = inheritedSettings.transparencySortMode; GUI.enabled &= allowProjectionParameters; inheritedSettings.projection = (tk2dCameraSettings.ProjectionType)EditorGUILayout.EnumPopup("Projection", inheritedSettings.projection); EditorGUI.indentLevel++; if (inheritedSettings.projection == tk2dCameraSettings.ProjectionType.Orthographic) { inheritedSettings.orthographicType = (tk2dCameraSettings.OrthographicType)EditorGUILayout.EnumPopup("Type", inheritedSettings.orthographicType); switch (inheritedSettings.orthographicType) { case tk2dCameraSettings.OrthographicType.OrthographicSize: inheritedSettings.orthographicSize = Mathf.Max(0.001f, EditorGUILayout.FloatField("Orthographic Size", inheritedSettings.orthographicSize)); break; case tk2dCameraSettings.OrthographicType.PixelsPerMeter: inheritedSettings.orthographicPixelsPerMeter = Mathf.Max(0.001f, EditorGUILayout.FloatField("Pixels per Meter", inheritedSettings.orthographicPixelsPerMeter)); break; } inheritedSettings.orthographicOrigin = (tk2dCameraSettings.OrthographicOrigin)EditorGUILayout.EnumPopup("Origin", inheritedSettings.orthographicOrigin); } else if (inheritedSettings.projection == tk2dCameraSettings.ProjectionType.Perspective) { inheritedSettings.fieldOfView = EditorGUILayout.Slider("Field of View", inheritedSettings.fieldOfView, 1, 179); transparencySortMode = (TransparencySortMode)EditorGUILayout.EnumPopup("Sort mode", transparencySortMode); } EditorGUI.indentLevel--; GUI.enabled = oldGuiEnabled; if (complete) { EditorGUILayout.Space(); GUILayout.Label("Clipping Planes"); GUILayout.BeginHorizontal(); GUILayout.Space(14); GUILayout.Label("Near"); if (m_Near != null) { EditorGUILayout.PropertyField(m_Near, GUIContent.none, GUILayout.Width(60)); } GUILayout.Label("Far"); if (m_Far != null) { EditorGUILayout.PropertyField(m_Far, GUIContent.none, GUILayout.Width(60)); } GUILayout.EndHorizontal(); cameraSettings.rect = EditorGUILayout.RectField("Normalized View Port Rect", cameraSettings.rect); EditorGUILayout.Space(); if (m_Depth != null) { EditorGUILayout.PropertyField(m_Depth); } if (m_RenderingPath != null) { EditorGUILayout.PropertyField(m_RenderingPath); } if (m_TargetTexture != null) { EditorGUILayout.PropertyField(m_TargetTexture); } if (m_HDR != null) { EditorGUILayout.PropertyField(m_HDR); } } cam.ApplyModifiedProperties(); so.ApplyModifiedProperties(); #if UNITY_5 if (transparencySortMode != inheritedSettings.transparencySortMode) { inheritedSettings.transparencySortMode = transparencySortMode; target.GetComponent <Camera>().transparencySortMode = transparencySortMode; // Change immediately in the editor EditorUtility.SetDirty(target); EditorUtility.SetDirty(target.GetComponent <Camera>()); } #else if (transparencySortMode != inheritedSettings.transparencySortMode) { inheritedSettings.transparencySortMode = transparencySortMode; target.camera.transparencySortMode = transparencySortMode; // Change immediately in the editor EditorUtility.SetDirty(target); EditorUtility.SetDirty(target.camera); } #endif }
void DrawEditorGUI() { if (GUILayout.Button("Commit")) { // Select all children, EXCLUDING self Transform[] allTransforms = batcher.transform.GetComponentsInChildren <Transform>(); List <Transform> tempAllTransforms = new List <Transform>(); foreach (Transform t in allTransforms) { if (t != batcher.transform) { tempAllTransforms.Add(t); } } allTransforms = tempAllTransforms.ToArray(); #if !(UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2) Renderer[] allRenderers = batcher.transform.GetComponentsInChildren <Renderer>(); List <Renderer> tempAllRenderers = new List <Renderer>(); foreach (Renderer r in allRenderers) { if (r != batcher.GetComponent <Renderer>()) { tempAllRenderers.Add(r); } } allRenderers = tempAllRenderers.ToArray(); if (allRenderers.Length > 0) { string sortingLayerName = allRenderers[0].sortingLayerName; int sortingOrder = allRenderers[0].sortingOrder; foreach (Renderer r in allRenderers) { if (sortingLayerName != r.sortingLayerName || sortingOrder != r.sortingOrder) { EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: Child objects use different sorting layer names and/or sorting orders.\n\nOnly one sorting layer and order is permitted in a static sprite batcher.", "Ok"); return; } } } #endif List <KeyValuePair <Transform, float> > tempList = new List <KeyValuePair <Transform, float> >(); // sort sprites, smaller to larger z if (batcher.CheckFlag(tk2dStaticSpriteBatcher.Flags.SortToCamera)) { tk2dCamera tk2dCam = tk2dCamera.CameraForLayer(batcher.gameObject.layer); Camera cam = tk2dCam ? tk2dCam.GetComponent <Camera>() : Camera.main; foreach (Transform t in allTransforms) { tempList.Add(new KeyValuePair <Transform, float>(t, cam.WorldToScreenPoint((t.GetComponent <Renderer>() != null) ? t.GetComponent <Renderer>().bounds.center : t.position).z)); } } else { foreach (Transform t in allTransforms) { tempList.Add(new KeyValuePair <Transform, float>(t, ((t.GetComponent <Renderer>() != null) ? t.GetComponent <Renderer>().bounds.center : t.position).z)); } } tempList.Sort((a, b) => a.Value.CompareTo(b.Value)); List <Transform> tr = new List <Transform>(); foreach (KeyValuePair <Transform, float> pair in tempList) { tr.Add(pair.Key); } allTransforms = tr.ToArray(); // and within the z sort by material if (allTransforms.Length == 0) { EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: No child objects found", "Ok"); return; } #if !(UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2) MeshCollider[] childMeshColliders = GetComponentsInChildrenExcludeSelf <MeshCollider>(batcher.transform); BoxCollider[] childBoxColliders = GetComponentsInChildrenExcludeSelf <BoxCollider>(batcher.transform); BoxCollider2D[] childBoxCollider2Ds = GetComponentsInChildrenExcludeSelf <BoxCollider2D>(batcher.transform); EdgeCollider2D[] childEdgeCollider2Ds = GetComponentsInChildrenExcludeSelf <EdgeCollider2D>(batcher.transform); PolygonCollider2D[] childPolygonCollider2Ds = GetComponentsInChildrenExcludeSelf <PolygonCollider2D>(batcher.transform); if ((childMeshColliders.Length > 0 || childBoxColliders.Length > 0) && (childBoxCollider2Ds.Length > 0 || childEdgeCollider2Ds.Length > 0 || childPolygonCollider2Ds.Length > 0)) { EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: Can't mix 2D and 3D colliders", "Ok"); return; } #endif Dictionary <Transform, int> batchedSpriteLookup = new Dictionary <Transform, int>(); batchedSpriteLookup[batcher.transform] = -1; Matrix4x4 batcherWorldToLocal = batcher.transform.worldToLocalMatrix; batcher.spriteCollection = null; batcher.batchedSprites = new tk2dBatchedSprite[allTransforms.Length]; List <tk2dTextMeshData> allTextMeshData = new List <tk2dTextMeshData>(); int currBatchedSprite = 0; foreach (var t in allTransforms) { tk2dBaseSprite baseSprite = t.GetComponent <tk2dBaseSprite>(); tk2dTextMesh textmesh = t.GetComponent <tk2dTextMesh>(); tk2dBatchedSprite bs = new tk2dBatchedSprite(); bs.name = t.gameObject.name; bs.position = t.localPosition; bs.rotation = t.localRotation; bs.relativeMatrix = batcherWorldToLocal * t.localToWorldMatrix; if (baseSprite) { bs.baseScale = Vector3.one; bs.localScale = new Vector3(t.localScale.x * baseSprite.scale.x, t.localScale.y * baseSprite.scale.y, t.localScale.z * baseSprite.scale.z); FillBatchedSprite(bs, t.gameObject); // temp redundant - just incase batcher expects to point to a valid one, somewhere we've missed batcher.spriteCollection = baseSprite.Collection; } else if (textmesh) { bs.spriteCollection = null; bs.type = tk2dBatchedSprite.Type.TextMesh; bs.color = textmesh.color; bs.baseScale = textmesh.scale; bs.renderLayer = textmesh.SortingOrder; bs.localScale = new Vector3(t.localScale.x * textmesh.scale.x, t.localScale.y * textmesh.scale.y, t.localScale.z * textmesh.scale.z); bs.FormattedText = textmesh.FormattedText; tk2dTextMeshData tmd = new tk2dTextMeshData(); tmd.Font = textmesh.font; tmd.Text = textmesh.text; tmd.color = textmesh.color; tmd.color2 = textmesh.color2; tmd.useGradient = textmesh.useGradient; tmd.textureGradient = textmesh.textureGradient; tmd.anchor = textmesh.anchor; tmd.kerning = textmesh.kerning; tmd.Spacing = textmesh.Spacing; tmd.LineSpacing = textmesh.LineSpacing; bs.xRefId = allTextMeshData.Count; allTextMeshData.Add(tmd); } else { // Empty GameObject bs.spriteId = -1; bs.baseScale = Vector3.one; bs.localScale = t.localScale; bs.type = tk2dBatchedSprite.Type.EmptyGameObject; } batchedSpriteLookup[t] = currBatchedSprite; batcher.batchedSprites[currBatchedSprite++] = bs; } batcher.allTextMeshData = allTextMeshData.ToArray(); int idx = 0; foreach (var t in allTransforms) { var bs = batcher.batchedSprites[idx]; bs.parentId = batchedSpriteLookup[t.parent]; t.parent = batcher.transform; // unparent ++idx; } //Transform[] directChildren = (from t in allTransforms where t.parent == batcher.transform select t).ToArray(); List <Transform> tempdirectChildren = new List <Transform>(); foreach (Transform t in allTransforms) { if (t.parent == batcher.transform) { tempdirectChildren.Add(t); } } Transform[] directChildren = tempdirectChildren.ToArray(); foreach (var t in directChildren) { GameObject.DestroyImmediate(t.gameObject); } Vector3 inverseScale = new Vector3(1.0f / batcher.scale.x, 1.0f / batcher.scale.y, 1.0f / batcher.scale.z); batcher.transform.localScale = Vector3.Scale(batcher.transform.localScale, inverseScale); batcher.Build(); EditorUtility.SetDirty(target); } }
void DrawCameraGUI(tk2dCamera target, bool complete) { bool allowProjectionParameters = target.SettingsRoot == target; bool oldGuiEnabled = GUI.enabled; SerializedObject so = this.serializedObject; SerializedObject cam = new SerializedObject( target.GetComponent<Camera>() ); SerializedProperty m_ClearFlags = cam.FindProperty("m_ClearFlags"); SerializedProperty m_BackGroundColor = cam.FindProperty("m_BackGroundColor"); SerializedProperty m_CullingMask = cam.FindProperty("m_CullingMask"); SerializedProperty m_TargetTexture = cam.FindProperty("m_TargetTexture"); SerializedProperty m_Near = cam.FindProperty("near clip plane"); SerializedProperty m_Far = cam.FindProperty("far clip plane"); SerializedProperty m_Depth = cam.FindProperty("m_Depth"); SerializedProperty m_RenderingPath = cam.FindProperty("m_RenderingPath"); SerializedProperty m_HDR = cam.FindProperty("m_HDR"); if (complete) { EditorGUILayout.PropertyField( m_ClearFlags ); EditorGUILayout.PropertyField( m_BackGroundColor ); EditorGUILayout.PropertyField( m_CullingMask ); EditorGUILayout.Space(); } tk2dCameraSettings cameraSettings = target.CameraSettings; tk2dCameraSettings inheritedSettings = target.SettingsRoot.CameraSettings; TransparencySortMode transparencySortMode = inheritedSettings.transparencySortMode; GUI.enabled &= allowProjectionParameters; inheritedSettings.projection = (tk2dCameraSettings.ProjectionType)EditorGUILayout.EnumPopup("Projection", inheritedSettings.projection); EditorGUI.indentLevel++; if (inheritedSettings.projection == tk2dCameraSettings.ProjectionType.Orthographic) { inheritedSettings.orthographicType = (tk2dCameraSettings.OrthographicType)EditorGUILayout.EnumPopup("Type", inheritedSettings.orthographicType); switch (inheritedSettings.orthographicType) { case tk2dCameraSettings.OrthographicType.OrthographicSize: inheritedSettings.orthographicSize = Mathf.Max( 0.001f, EditorGUILayout.FloatField("Orthographic Size", inheritedSettings.orthographicSize) ); break; case tk2dCameraSettings.OrthographicType.PixelsPerMeter: inheritedSettings.orthographicPixelsPerMeter = Mathf.Max( 0.001f, EditorGUILayout.FloatField("Pixels per Meter", inheritedSettings.orthographicPixelsPerMeter) ); break; } inheritedSettings.orthographicOrigin = (tk2dCameraSettings.OrthographicOrigin)EditorGUILayout.EnumPopup("Origin", inheritedSettings.orthographicOrigin); } else if (inheritedSettings.projection == tk2dCameraSettings.ProjectionType.Perspective) { inheritedSettings.fieldOfView = EditorGUILayout.Slider("Field of View", inheritedSettings.fieldOfView, 1, 179); transparencySortMode = (TransparencySortMode)EditorGUILayout.EnumPopup("Sort mode", transparencySortMode); } EditorGUI.indentLevel--; GUI.enabled = oldGuiEnabled; if (complete) { EditorGUILayout.Space(); GUILayout.Label("Clipping Planes"); GUILayout.BeginHorizontal(); GUILayout.Space(14); GUILayout.Label("Near"); if (m_Near != null) EditorGUILayout.PropertyField(m_Near, GUIContent.none, GUILayout.Width(60) ); GUILayout.Label("Far"); if (m_Far != null) EditorGUILayout.PropertyField(m_Far, GUIContent.none, GUILayout.Width(60) ); GUILayout.EndHorizontal(); cameraSettings.rect = EditorGUILayout.RectField("Normalized View Port Rect", cameraSettings.rect); EditorGUILayout.Space(); if (m_Depth != null) EditorGUILayout.PropertyField(m_Depth); if (m_RenderingPath != null) EditorGUILayout.PropertyField(m_RenderingPath); if (m_TargetTexture != null) EditorGUILayout.PropertyField(m_TargetTexture); if (m_HDR != null) EditorGUILayout.PropertyField(m_HDR); } cam.ApplyModifiedProperties(); so.ApplyModifiedProperties(); if (transparencySortMode != inheritedSettings.transparencySortMode) { inheritedSettings.transparencySortMode = transparencySortMode; target.GetComponent<Camera>().transparencySortMode = transparencySortMode; // Change immediately in the editor EditorUtility.SetDirty(target); EditorUtility.SetDirty(target.GetComponent<Camera>()); } }