// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void OnPreRender() { // ======================================================== // update sprites // ======================================================== for (int i = 0; i < sprites.Count; ++i) { exPlane sp = sprites[i]; // NOTE: it is possible the sprite has been destroyed first if (sp != null) { if (sp.updateFlags != exPlane.UpdateFlags.None) { sp.Commit(); sp.updateFlags = exPlane.UpdateFlags.None; } sp.inCommitList = false; } } sprites.Clear(); // ======================================================== // update clip items // ======================================================== for (int i = 0; i < clippingList.Count; ++i) { exClipping clipping = clippingList[i]; if (clipping.enabled) { clipping.CommitMaterialProperties(); } } }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected void OnEnable() { if (target != editPlane) { editPlane = target as exPlane; if (editPlane.renderer != null) { EditorUtility.SetSelectedWireframeHidden(editPlane.renderer, true); // EditorUtility.SetSelectedWireframeHidden(editPlane.renderer, false); // DEBUG } // get trans2d if (editPlane.GetComponent <exScreenPosition>() != null) { trans2d = Transform2D.Screen; } else if (editPlane.GetComponent <exViewportPosition>() != null) { trans2d = Transform2D.Viewport; } else { trans2d = Transform2D.None; } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void InsertPlane(int _idx, exPlane _plane) { // we already have this in clipping list if (HasPlaneInfo(_plane)) { return; } if (_plane.clippingPlane != null) { _plane.clippingPlane.RemovePlane(_plane); } InsertPlaneInfo(_idx, _plane); _plane.clippingPlane = this; // if we are in player or if we are running in editor if (Application.isPlaying) { exClipping clipPlane = _plane as exClipping; // if this is not a clip plane if (clipPlane == null) { ApplyClipMaterial(_plane); } } }
public static Vector3 GetOffset(this exPlane _plane) { Vector2 anchorOffset = Vector2.zero; Vector2 size = new Vector2(_plane.width, _plane.height); switch (_plane.anchor) { case Anchor.TopLeft: anchorOffset = new Vector3(-size.x * 0.5f, size.y * 0.5f, 0.0f); break; case Anchor.TopCenter: anchorOffset = new Vector3(0.0f, size.y * 0.5f, 0.0f); break; case Anchor.TopRight: anchorOffset = new Vector3(size.x * 0.5f, size.y * 0.5f, 0.0f); break; case Anchor.MidLeft: anchorOffset = new Vector3(-size.x * 0.5f, 0.0f, 0.0f); break; case Anchor.MidCenter: anchorOffset = new Vector3(0.0f, 0.0f, 0.0f); break; case Anchor.MidRight: anchorOffset = new Vector3(size.x * 0.5f, 0.0f, 0.0f); break; case Anchor.BotLeft: anchorOffset = new Vector3(-size.x * 0.5f, -size.y * 0.5f, 0.0f); break; case Anchor.BotCenter: anchorOffset = new Vector3(0.0f, -size.y * 0.5f, 0.0f); break; case Anchor.BotRight: anchorOffset = new Vector3(size.x * 0.5f, -size.y * 0.5f, 0.0f); break; } Vector3 scaledOffset = _plane.offset + anchorOffset; Vector3 lossyScale = _plane.transform.lossyScale; scaledOffset.x *= lossyScale.x; scaledOffset.y *= lossyScale.y; return(anchorOffset); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void OnEnable() { if (plane == null) { plane = GetComponent <exPlane>(); } }
// ------------------------------------------------------------------ // Apply exSprite or ex3DSprite change // ------------------------------------------------------------------ public static void ApplyPlaneScale(exPlane _plane, Vector3 _size, Vector3 _center) { _plane.width = _size.x; _plane.height = _size.y; Vector3 offset = new Vector3( -_plane.offset.x, -_plane.offset.y, 0.0f ); Vector3 anchorOffset = Vector3.zero; switch (_plane.anchor) { case Anchor.TopLeft: anchorOffset = new Vector3( -_size.x*0.5f, _size.y*0.5f, 0.0f ); break; case Anchor.TopCenter: anchorOffset = new Vector3( 0.0f, _size.y*0.5f, 0.0f ); break; case Anchor.TopRight: anchorOffset = new Vector3( _size.x*0.5f, _size.y*0.5f, 0.0f ); break; case Anchor.MidLeft: anchorOffset = new Vector3( -_size.x*0.5f, 0.0f, 0.0f ); break; case Anchor.MidCenter: anchorOffset = new Vector3( 0.0f, 0.0f, 0.0f ); break; case Anchor.MidRight: anchorOffset = new Vector3( _size.x*0.5f, 0.0f, 0.0f ); break; case Anchor.BotLeft: anchorOffset = new Vector3( -_size.x*0.5f, -_size.y*0.5f, 0.0f ); break; case Anchor.BotCenter: anchorOffset = new Vector3( 0.0f, -_size.y*0.5f, 0.0f ); break; case Anchor.BotRight: anchorOffset = new Vector3( _size.x*0.5f, -_size.y*0.5f, 0.0f ); break; } Vector3 scaledOffset = offset + anchorOffset; Transform trans = _plane.transform; Vector3 lossyScale = trans.lossyScale; scaledOffset.x *= lossyScale.x; scaledOffset.y *= lossyScale.y; Vector3 newPos = _center + trans.rotation * scaledOffset; Vector3 localPos = trans.InverseTransformPoint (newPos); localPos.z = 0; // keep z unchagned trans.position = trans.TransformPoint (localPos); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void UpdatePlane(exPlane _plane) { PlaneInfo planeInfo = null; for (int i = 0; i < planeInfoList.Count; ++i) { if (planeInfoList[i].plane == _plane) { planeInfo = planeInfoList[i]; break; } } if (planeInfo == null) { Debug.LogWarning("Can't find plane info of " + _plane.name); return; } // update plane info material if (_plane.GetComponent <Renderer>() != null) { planeInfo.material = _plane.GetComponent <Renderer>().sharedMaterial; } // if we are in player or if we are running in editor if (Application.isPlaying) { exClipping clipPlane = _plane as exClipping; // if this is not a clip plane if (clipPlane == null) { ApplyClipMaterial(_plane); } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void AddToCommitList( exPlane _plane ) { if ( _plane.inCommitList == false ) { sprites.Add (_plane); _plane.inCommitList = true; } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected virtual void DoInspectorGUI() { // width // EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(widthProp, new GUIContent("Width")); // if ( EditorGUI.EndChangeCheck() ) { // foreach ( Object obj in serializedObject.targetObjects ) { // exPlane plane = obj as exPlane; // if ( plane ) { // plane.width = Mathf.Max(widthProp.floatValue, 0f); // EditorUtility.SetDirty(plane); // } // } // } // height // EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(heightProp, new GUIContent("Height")); // if ( EditorGUI.EndChangeCheck() ) { // foreach ( Object obj in serializedObject.targetObjects ) { // exPlane plane = obj as exPlane; // if ( plane ) { // plane.height = Mathf.Max(heightProp.floatValue, 0f); // EditorUtility.SetDirty(plane); // } // } // } // anchor EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(anchorProp, new GUIContent("Anchor")); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { exPlane plane = obj as exPlane; if (plane) { plane.anchor = (Anchor)anchorProp.enumValueIndex; EditorUtility.SetDirty(plane); } } } // offset EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(offsetProp, new GUIContent("Offset"), true); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { exPlane plane = obj as exPlane; if (plane) { plane.offset = offsetProp.vector2Value; EditorUtility.SetDirty(plane); } } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void OnEnable() { if (plane == null) { plane = GetComponent <exPlane>(); } UpdateDepth(); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void AddToCommitList(exPlane _plane) { if (_plane.inCommitList == false) { sprites.Add(_plane); _plane.inCommitList = true; } }
// ------------------------------------------------------------------ /// \param _clipping the clipping plane /// \param _plane the plane to add to clip /// add plane to clipping list // ------------------------------------------------------------------ public static void AddPlaneInEditor( this exClipping _clipping, exPlane _plane ) { _clipping.AddPlane (_plane); exClipping clipPlane = _plane as exClipping; // if this is not a clip plane if ( clipPlane == null ) { ApplyClipMaterialInEditor ( _clipping, _plane ); } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public new void OnSceneGUI() { exUIControl ctrl = target as exUIControl; Vector3[] vertices = ctrl.GetLocalVertices(); if (vertices.Length > 0) { Rect aabb = exGeometryUtility.GetAABoundingRect(vertices); Matrix4x4 l2w = ctrl.transform.localToWorldMatrix; // draw control rect vertices = new Vector3[4] { l2w.MultiplyPoint3x4(new Vector3(aabb.xMin, aabb.yMin, 0)), l2w.MultiplyPoint3x4(new Vector3(aabb.xMin, aabb.yMax, 0)), l2w.MultiplyPoint3x4(new Vector3(aabb.xMax, aabb.yMax, 0)), l2w.MultiplyPoint3x4(new Vector3(aabb.xMax, aabb.yMin, 0)), }; exEditorUtility.GL_DrawRectLine(vertices, new Color(1.0f, 0.0f, 0.5f, 1.0f), true); // draw scroll-view content exUIScrollView scrollView = ctrl as exUIScrollView; if (scrollView != null) { aabb.width = scrollView.contentSize.x; aabb.yMin = aabb.yMax - scrollView.contentSize.y; float contentX = (scrollView.horizontalContentDir == exUIScrollView.ContentDirection.LeftToRight) ? 0.0f : (scrollView.contentSize.x - scrollView.width); float contentY = (scrollView.verticalContentDir == exUIScrollView.ContentDirection.TopToBottom) ? 0.0f : (scrollView.contentSize.y - scrollView.height); aabb.center += scrollView.scrollOffset; aabb.center += new Vector2(contentX, contentY); vertices = new Vector3[4] { l2w.MultiplyPoint3x4(new Vector3(aabb.xMin, aabb.yMin, 0)), l2w.MultiplyPoint3x4(new Vector3(aabb.xMin, aabb.yMax, 0)), l2w.MultiplyPoint3x4(new Vector3(aabb.xMax, aabb.yMax, 0)), l2w.MultiplyPoint3x4(new Vector3(aabb.xMax, aabb.yMin, 0)), }; exEditorUtility.GL_DrawRectLine(vertices, new Color(0.0f, 0.5f, 1.0f, 1.0f), true); } } exPlane plane = target as exPlane; if (plane.hasSprite == false) { Vector3 size; Vector3 center; bool changed = ProcessSceneEditorHandles(out size, out center); if (changed) { ApplyPlaneScale(plane, size, center); } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public bool HasPlaneInfo(exPlane _plane) { for (int i = 0; i < planeInfoList.Count; ++i) { if (planeInfoList[i].plane == _plane) { return(true); } } return(false); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected void AddPlaneInfo(exPlane _plane) { PlaneInfo planeInfo = new PlaneInfo(); planeInfo.plane = _plane; if (_plane.GetComponent <Renderer>() != null) { planeInfo.material = _plane.GetComponent <Renderer>().sharedMaterial; } planeInfoList.Add(planeInfo); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected void InsertPlaneInfo(int _idx, exPlane _plane) { PlaneInfo planeInfo = new PlaneInfo(); planeInfo.plane = _plane; if (_plane.GetComponent <Renderer>() != null) { planeInfo.material = _plane.GetComponent <Renderer>().sharedMaterial; } planeInfoList.Insert(_idx, planeInfo); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected void AddPlaneInfo(exPlane _plane) { PlaneInfo planeInfo = new PlaneInfo(); planeInfo.plane = _plane; if (_plane.renderer != null) { planeInfo.material = _plane.renderer.sharedMaterial; } planeInfoList.Add(planeInfo); }
// ------------------------------------------------------------------ /// \param _plane the in plane /// \param _camera the in camera /// \param _viewport_x the viewport x position /// \param _viewport_y the viewport y position /// \return the world position /// Convert the viewport position to world position in _camera depends on exPlane.plane // ------------------------------------------------------------------ public static Vector3 ViewportToWorldPoint(this exPlane _plane, Camera _camera, float _viewport_x, float _viewport_y) { Vector3 result = Vector3.zero; result = _camera.ViewportToWorldPoint(new Vector3(_viewport_x, _viewport_y, _plane.transform.position.z)); result.z = _plane.transform.position.z; return(result); }
// ------------------------------------------------------------------ /// \param _clipping the clipping plane /// \param _plane the plane to add to clip /// add plane to clipping list // ------------------------------------------------------------------ public static void AddPlaneInEditor(this exClipping _clipping, exPlane _plane) { _clipping.AddPlane(_plane); exClipping clipPlane = _plane as exClipping; // if this is not a clip plane if (clipPlane == null) { ApplyClipMaterialInEditor(_clipping, _plane); } }
// ------------------------------------------------------------------ /// \param _plane the in plane /// \param _length the length of the collider /// sync the collider size // ------------------------------------------------------------------ public static void UpdateColliderSize(this exPlane _plane, float _length) { Collider collider = _plane.collider; // update box collider if (collider is BoxCollider) { BoxCollider boxCollider = collider as BoxCollider; Rect boundingRect = _plane.boundingRect; float center_x = _plane.boundingRect.x + _plane.boundingRect.width * 0.5f; float center_y = _plane.boundingRect.y + _plane.boundingRect.height * 0.5f; boxCollider.center = new Vector3(center_x, center_y, boxCollider.center.z); boxCollider.size = new Vector3(boundingRect.width, boundingRect.height, _length); // DELME { // switch ( _plane.plane ) { // case exSprite.Plane.XY: // boxCollider.center = new Vector3 ( center_x, center_y, boxCollider.center.z ); // boxCollider.size = new Vector3( boundingRect.width, boundingRect.height, _length ); // break; // case exSprite.Plane.XZ: // boxCollider.center = new Vector3 ( center_x, boxCollider.center.y, center_y ); // boxCollider.size = new Vector3( boundingRect.width, _length, boundingRect.height ); // break; // case exSprite.Plane.ZY: // boxCollider.center = new Vector3 ( boxCollider.center.x, center_y, center_x ); // boxCollider.size = new Vector3( _length, boundingRect.height, boundingRect.width ); // break; // } // } DELME end return; } // update mesh collider if (collider is MeshCollider) { MeshFilter meshFilter = _plane.meshFilter; if (meshFilter) { Mesh mesh = meshFilter.sharedMesh; MeshCollider meshCollider = collider as MeshCollider; // NOTE: only in this way, mesh collider changes meshCollider.sharedMesh = null; meshCollider.sharedMesh = mesh; } return; } }
// ------------------------------------------------------------------ /// Update the length of the collider if we use auto length // ------------------------------------------------------------------ public void UpdateCenter() { if (autoLength_ == false || collider == null) { return; } if (plane == null) { plane = GetComponent <exPlane>(); } // update box collider if (collider is BoxCollider) { BoxCollider boxCollider = collider as BoxCollider; Camera camera = plane.renderCamera; float myLength = camera.farClipPlane - camera.nearClipPlane; float thick = camera.farClipPlane - camera.nearClipPlane; float offset = myLength * 0.5f + (camera.transform.position.z + camera.nearClipPlane) - transform.position.z; boxCollider.center = new Vector3(boxCollider.center.x, boxCollider.center.y, offset); boxCollider.size = new Vector3(boxCollider.size.x, boxCollider.size.y, thick); // DELME { // switch ( plane.plane ) { // case exSprite.Plane.XY: // offset = myLength * 0.5f + (camera.transform.position.z + camera.nearClipPlane) - transform.position.z; // boxCollider.center = new Vector3( boxCollider.center.x, boxCollider.center.y, offset ); // boxCollider.size = new Vector3( boxCollider.size.x, boxCollider.size.y, thick ); // break; // case exSprite.Plane.XZ: // offset = myLength * 0.5f + transform.position.y - (camera.transform.position.y + camera.nearClipPlane); // boxCollider.center = new Vector3( boxCollider.center.x, -offset, boxCollider.center.z ); // boxCollider.size = new Vector3( boxCollider.size.x, thick, boxCollider.size.z ); // break; // case exSprite.Plane.ZY: // offset = myLength * 0.5f + transform.position.x - (camera.transform.position.x + camera.nearClipPlane); // boxCollider.center = new Vector3( -offset, boxCollider.center.x, boxCollider.center.z ); // boxCollider.size = new Vector3( thick, boxCollider.size.y, boxCollider.size.z ); // break; // } // } DELME end return; } }
// ------------------------------------------------------------------ /// Update the size BoxCollider to fit the size of sprite, only affect /// when autoResizeCollision is true // ------------------------------------------------------------------ public void UpdateSize() { if (plane == null) { plane = GetComponent <exPlane>(); } if (collider == null || autoResizeCollision == false) { return; } plane.UpdateColliderSize(length_); }
// ------------------------------------------------------------------ /// OnEnable functoin inherit from exPlane. /// When enabled set to true, it will enable all the item in the planes // ------------------------------------------------------------------ override protected void OnEnable() { base.OnEnable(); for (int i = 0; i < planes.Count; ++i) { exPlane p = planes[i]; if (p == null) { planes.RemoveAt(i); --i; continue; } p.enabled = true; } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void OnSceneGUI() { exPlane plane = target as exPlane; exEditorUtility.GL_DrawWireFrame(plane, new Color(1.0f, 0.0f, 0.5f, 1.0f), false); if (plane.hasSprite == false) { Vector3 size; Vector3 center; bool changed = ProcessSceneEditorHandles(out size, out center); if (changed) { exPlaneInspector.ApplyPlaneScale(plane, size, center); } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ static void ApplyClipMaterialInEditor( this exClipping _clipping, exPlane _plane ) { Renderer r = _plane.renderer; if ( r != null && r.sharedMaterial != null ) { Texture2D texture = r.sharedMaterial.mainTexture as Texture2D; if ( _clipping.textureToClipMaterialTable.ContainsKey(texture) == false ) { r.sharedMaterial = exEditorHelper.GetDefaultMaterial ( texture, texture.name + "-clipping-" + Mathf.Abs(_clipping.GetInstanceID()), "ex2D/Alpha Blended (Clipping)" ); _clipping.AddClipMaterial ( texture, r.sharedMaterial ); } else { r.sharedMaterial = _clipping.textureToClipMaterialTable[texture]; } } }
// ------------------------------------------------------------------ /// OnEnable functoin inherit from exPlane. /// When enabled set to true, it will enable all the item in the planeInfoList // ------------------------------------------------------------------ protected new void OnEnable() { base.OnEnable(); Init(); // for (int i = 0; i < planeInfoList.Count; ++i) { PlaneInfo pi = planeInfoList[i]; exPlane plane = pi.plane; if (plane && plane.GetComponent <Renderer>() && plane.GetComponent <Renderer>().sharedMaterial) { Texture2D texture = plane.GetComponent <Renderer>().sharedMaterial.mainTexture as Texture2D; plane.GetComponent <Renderer>().material = textureToClipMaterialTable[texture]; } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected bool RemovePlaneInfo(exPlane _plane) { for (int i = 0; i < planeInfoList.Count; ++i) { PlaneInfo pi = planeInfoList[i]; if (_plane == pi.plane) { if (_plane.GetComponent <Renderer>() != null) { _plane.GetComponent <Renderer>().sharedMaterial = pi.material; } planeInfoList.RemoveAt(i); return(true); } } return(false); }
// ------------------------------------------------------------------ /// remove plane from clipping list // ------------------------------------------------------------------ public bool RemovePlane(exPlane _plane) { bool result = RemovePlaneInfo(_plane); if (result) { _plane.clippingPlane = null; if (isDyanmic || (Application.isPlaying == false)) { if (_plane.GetComponent <Renderer>() && _plane.GetComponent <Renderer>().sharedMaterial) { CheckAndRemoveClipMaterial(_plane.GetComponent <Renderer>().sharedMaterial.mainTexture as Texture2D); } } } return(result); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void RecursivelyAddToClip(Transform _t) { foreach (Transform child in _t) { exPlane plane = child.GetComponent <exPlane>(); if (plane != null) { planes.Add(plane); exSoftClip clipPlane = plane as exSoftClip; // if this is a clip plane, add child to it if (clipPlane != null) { clipPlane.UpdateClipList(); continue; } } RecursivelyAddToClip(child); } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public override void OnInspectorGUI() { serializedObject.Update(); // NOTE DANGER: Unity didn't allow user change script in custom inspector { SerializedProperty scriptProp = serializedObject.FindProperty("m_Script"); EditorGUILayout.PropertyField(scriptProp); // } DANGER end DoInspectorGUI(); serializedObject.ApplyModifiedProperties(); // if we have sprite exPlane targetPlane = target as exPlane; if (targetPlane.hasSprite) { exSpriteBase spriteBase = targetPlane.GetComponent <exSpriteBase>(); // if ( targetPlane.width != spriteBase.width ) { // targetPlane.width = spriteBase.width; // EditorUtility.SetDirty(targetPlane); // } // if ( targetPlane.height != spriteBase.height ) { // targetPlane.height = spriteBase.height; // EditorUtility.SetDirty(targetPlane); // } if (targetPlane.anchor != spriteBase.anchor) { targetPlane.anchor = spriteBase.anchor; EditorUtility.SetDirty(targetPlane); } if (targetPlane.offset != spriteBase.offset) { targetPlane.offset = spriteBase.offset; EditorUtility.SetDirty(targetPlane); } } }
// ------------------------------------------------------------------ // Apply exSprite or ex3DSprite change // ------------------------------------------------------------------ public static void ApplyPlaneScale(exPlane _plane, Vector3 _size, Vector3 _center) { _plane.width = _size.x; _plane.height = _size.y; Vector3 offset = new Vector3(-_plane.offset.x, -_plane.offset.y, 0.0f); Vector3 anchorOffset = Vector3.zero; switch (_plane.anchor) { case Anchor.TopLeft: anchorOffset = new Vector3(-_size.x * 0.5f, _size.y * 0.5f, 0.0f); break; case Anchor.TopCenter: anchorOffset = new Vector3(0.0f, _size.y * 0.5f, 0.0f); break; case Anchor.TopRight: anchorOffset = new Vector3(_size.x * 0.5f, _size.y * 0.5f, 0.0f); break; case Anchor.MidLeft: anchorOffset = new Vector3(-_size.x * 0.5f, 0.0f, 0.0f); break; case Anchor.MidCenter: anchorOffset = new Vector3(0.0f, 0.0f, 0.0f); break; case Anchor.MidRight: anchorOffset = new Vector3(_size.x * 0.5f, 0.0f, 0.0f); break; case Anchor.BotLeft: anchorOffset = new Vector3(-_size.x * 0.5f, -_size.y * 0.5f, 0.0f); break; case Anchor.BotCenter: anchorOffset = new Vector3(0.0f, -_size.y * 0.5f, 0.0f); break; case Anchor.BotRight: anchorOffset = new Vector3(_size.x * 0.5f, -_size.y * 0.5f, 0.0f); break; } Vector3 scaledOffset = offset + anchorOffset; Transform trans = _plane.transform; Vector3 lossyScale = trans.lossyScale; scaledOffset.x *= lossyScale.x; scaledOffset.y *= lossyScale.y; Vector3 newPos = _center + trans.rotation * scaledOffset; Vector3 localPos = trans.InverseTransformPoint(newPos); localPos.z = 0; // keep z unchagned trans.position = trans.TransformPoint(localPos); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ static void ApplyClipMaterialInEditor(this exClipping _clipping, exPlane _plane) { Renderer r = _plane.renderer; if (r != null && r.sharedMaterial != null) { Texture2D texture = r.sharedMaterial.mainTexture as Texture2D; if (_clipping.textureToClipMaterialTable.ContainsKey(texture) == false) { r.sharedMaterial = exEditorHelper.GetDefaultMaterial(texture, texture.name + "-clipping-" + Mathf.Abs(_clipping.GetInstanceID()), "ex2D/Alpha Blended (Clipping)"); _clipping.AddClipMaterial(texture, r.sharedMaterial); } else { r.sharedMaterial = _clipping.textureToClipMaterialTable[texture]; } } }
// ------------------------------------------------------------------ /// \param _plane the in plane /// \param _dx the delta x /// \param _dy the delta y /// translate the 2D pos by plane // ------------------------------------------------------------------ public static void Translate(this exPlane _plane, float _dx, float _dy) { _plane.transform.Translate(_dx, _dy, 0.0f); // DELME { // switch ( _plane.plane ) { // case exPlane.Plane.XY: // _plane.transform.Translate ( _dx, _dy, 0.0f ); // break; // case exPlane.Plane.XZ: // _plane.transform.Translate ( _dx, 0.0f, _dy ); // break; // case exPlane.Plane.ZY: // _plane.transform.Translate ( 0.0f, _dy, _dx ); // break; // } // } DELME end }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected void ApplyClipMaterial(exPlane _plane) { Renderer r = _plane.GetComponent <Renderer>(); if (r != null && r.sharedMaterial != null) { Texture2D texture = r.sharedMaterial.mainTexture as Texture2D; if (texture != null) { if (textureToClipMaterialTable.ContainsKey(texture) == false) { r.material = new Material(Shader.Find("ex2D/Alpha Blended (Clipping)")); r.material.mainTexture = texture; AddClipMaterial(texture, r.material); } else { r.material = textureToClipMaterialTable[texture]; } } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void OnEnable () { if ( plane == null ) { plane = GetComponent<exPlane>(); } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public bool HasPlaneInfo( exPlane _plane ) { for ( int i = 0; i < planeInfoList.Count; ++i ) { if ( planeInfoList[i].plane == _plane ) return true; } return false; }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void OnEnable () { if ( plane == null ) { plane = GetComponent<exPlane>(); } UpdateDepth (); }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected void OnEnable() { if ( target != editPlane ) { editPlane = target as exPlane; if ( editPlane.renderer != null ) { EditorUtility.SetSelectedWireframeHidden(editPlane.renderer, true); // EditorUtility.SetSelectedWireframeHidden(editPlane.renderer, false); // DEBUG } // get trans2d if ( editPlane.GetComponent<exScreenPosition>() != null ) { trans2d = Transform2D.Screen; } else if ( editPlane.GetComponent<exViewportPosition>() != null ) { trans2d = Transform2D.Viewport; } else { trans2d = Transform2D.None; } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void UpdatePlane( exPlane _plane ) { PlaneInfo planeInfo = null; for ( int i = 0; i < planeInfoList.Count; ++i ) { if ( planeInfoList[i].plane == _plane ) { planeInfo = planeInfoList[i]; break; } } if ( planeInfo == null ) { Debug.LogWarning( "Can't find plane info of " + _plane.name ); return; } // update plane info material if ( _plane.renderer != null ) planeInfo.material = _plane.renderer.sharedMaterial; // if we are in player or if we are running in editor if ( Application.isPlaying ) { exClipping clipPlane = _plane as exClipping; // if this is not a clip plane if ( clipPlane == null ) { ApplyClipMaterial (_plane); } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected void AddPlaneInfo( exPlane _plane ) { PlaneInfo planeInfo = new PlaneInfo(); planeInfo.plane = _plane; if ( _plane.renderer != null ) planeInfo.material = _plane.renderer.sharedMaterial; planeInfoList.Add(planeInfo); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected void ApplyClipMaterial( exPlane _plane ) { Renderer r = _plane.renderer; if ( r != null && r.sharedMaterial != null ) { Texture2D texture = r.sharedMaterial.mainTexture as Texture2D; if ( texture != null ) { if ( textureToClipMaterialTable.ContainsKey(texture) == false ) { r.material = new Material( Shader.Find("ex2D/Alpha Blended (Clipping)") ); r.material.mainTexture = texture; AddClipMaterial ( texture, r.material ); } else { r.material = textureToClipMaterialTable[texture]; } } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected void InsertPlaneInfo( int _idx, exPlane _plane ) { PlaneInfo planeInfo = new PlaneInfo(); planeInfo.plane = _plane; if ( _plane.renderer != null ) planeInfo.material = _plane.renderer.sharedMaterial; planeInfoList.Insert(_idx,planeInfo); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected bool RemovePlaneInfo( exPlane _plane ) { for ( int i = 0; i < planeInfoList.Count; ++i ) { PlaneInfo pi = planeInfoList[i]; if ( _plane == pi.plane ) { if ( _plane.renderer != null ) _plane.renderer.sharedMaterial = pi.material; planeInfoList.RemoveAt(i); return true; } } return false; }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void RemovePlaneInEditor( this exClipping _clipping, exPlane _plane ) { _clipping.RemovePlane (_plane); }
// ------------------------------------------------------------------ /// remove plane from clipping list // ------------------------------------------------------------------ public bool RemovePlane( exPlane _plane ) { bool result = RemovePlaneInfo(_plane); if ( result ) { _plane.clippingPlane = null; if ( isDyanmic || (Application.isPlaying == false ) ) { if ( _plane.renderer && _plane.renderer.sharedMaterial ) { CheckAndRemoveClipMaterial(_plane.renderer.sharedMaterial.mainTexture as Texture2D); } } } return result; }
// ------------------------------------------------------------------ /// \param _plane the in plane /// \param _camera the in camera /// \param _screen_x the screen x position /// \param _screen_y the screen y position /// \return the world position /// Convert the screen position to world position in _camera depends on exPlane.plane // ------------------------------------------------------------------ public static Vector3 ScreenToWorldPoint( this exPlane _plane, Camera _camera, exPlane.Anchor _anchor, float _screen_x, float _screen_y ) { Vector3 result = Vector3.zero; float offsetX = 0.0f; float offsetY = 0.0f; // ======================================================== // anchor // ======================================================== switch ( _anchor ) { // case exPlane.Anchor.TopLeft: offsetX = _screen_x; offsetY = Screen.height - _screen_y; break; case exPlane.Anchor.TopCenter: offsetX = Screen.width * 0.5f + _screen_x; offsetY = Screen.height - _screen_y; break; case exPlane.Anchor.TopRight: offsetX = Screen.width - _screen_x; offsetY = Screen.height - _screen_y; break; // case exPlane.Anchor.MidLeft: offsetX = _screen_x; offsetY = Screen.height * 0.5f + _screen_y; break; case exPlane.Anchor.MidCenter: offsetX = Screen.width * 0.5f + _screen_x; offsetY = Screen.height * 0.5f + _screen_y; break; case exPlane.Anchor.MidRight: offsetX = Screen.width - _screen_x; offsetY = Screen.height * 0.5f + _screen_y; break; // case exPlane.Anchor.BotLeft: offsetX = _screen_x; offsetY = _screen_y; break; case exPlane.Anchor.BotCenter: offsetX = Screen.width * 0.5f + _screen_x; offsetY = _screen_y; break; case exPlane.Anchor.BotRight: offsetX = Screen.width - _screen_x; offsetY = _screen_y; break; } // ======================================================== // plane // ======================================================== switch ( _plane.plane ) { case exPlane.Plane.XY: result = _camera.ScreenToWorldPoint( new Vector3(offsetX, offsetY, 0.0f) ); result.z = _plane.transform.position.z; break; case exPlane.Plane.XZ: result = _camera.ScreenToWorldPoint( new Vector3(offsetX, offsetY, 0.0f) ); result.y = _plane.transform.position.y; break; case exPlane.Plane.ZY: result = _camera.ScreenToWorldPoint( new Vector3(offsetX, offsetY, 0.0f) ); result.x = _plane.transform.position.x; break; } return result; }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// void Awake() { plane = GetComponent<exPlane>(); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void InsertPlane( int _idx, exPlane _plane ) { // we already have this in clipping list if ( HasPlaneInfo(_plane) ) return; if ( _plane.clippingPlane != null ) { _plane.clippingPlane.RemovePlane (_plane); } InsertPlaneInfo(_idx,_plane); _plane.clippingPlane = this; // if we are in player or if we are running in editor if ( Application.isPlaying ) { exClipping clipPlane = _plane as exClipping; // if this is not a clip plane if ( clipPlane == null ) { ApplyClipMaterial (_plane); } } }
// ------------------------------------------------------------------ /// Update the length of the collider if we use auto length // ------------------------------------------------------------------ public void UpdateCenter () { if ( autoLength_ == false || collider == null ) return; if ( plane == null ) plane = GetComponent<exPlane>(); // update box collider if ( collider is BoxCollider ) { BoxCollider boxCollider = collider as BoxCollider; Camera camera = plane.renderCamera; float myLength = camera.farClipPlane - camera.nearClipPlane; float thick = camera.farClipPlane - camera.nearClipPlane; float offset = myLength * 0.5f + (camera.transform.position.z + camera.nearClipPlane) - transform.position.z; boxCollider.center = new Vector3( boxCollider.center.x, boxCollider.center.y, offset ); boxCollider.size = new Vector3( boxCollider.size.x, boxCollider.size.y, thick ); // DELME { // switch ( plane.plane ) { // case exSprite.Plane.XY: // offset = myLength * 0.5f + (camera.transform.position.z + camera.nearClipPlane) - transform.position.z; // boxCollider.center = new Vector3( boxCollider.center.x, boxCollider.center.y, offset ); // boxCollider.size = new Vector3( boxCollider.size.x, boxCollider.size.y, thick ); // break; // case exSprite.Plane.XZ: // offset = myLength * 0.5f + transform.position.y - (camera.transform.position.y + camera.nearClipPlane); // boxCollider.center = new Vector3( boxCollider.center.x, -offset, boxCollider.center.z ); // boxCollider.size = new Vector3( boxCollider.size.x, thick, boxCollider.size.z ); // break; // case exSprite.Plane.ZY: // offset = myLength * 0.5f + transform.position.x - (camera.transform.position.x + camera.nearClipPlane); // boxCollider.center = new Vector3( -offset, boxCollider.center.x, boxCollider.center.z ); // boxCollider.size = new Vector3( thick, boxCollider.size.y, boxCollider.size.z ); // break; // } // } DELME end return; } }
// ------------------------------------------------------------------ /// Update the size BoxCollider to fit the size of sprite, only affect /// when autoResizeCollision is true // ------------------------------------------------------------------ public void UpdateSize () { if ( plane == null ) plane = GetComponent<exPlane>(); if ( collider == null || autoResizeCollision == false ) return; plane.UpdateColliderSize(length_); }