// Allows drag & dropping of this sprite to change its clip in the editor protected void LateUpdate() { // 这里的处理方式和exLayeredSprite.LateUpdate一样 // 如果exClipping不单单clip子物体,那就会复杂很多 if (UnityEditor.EditorApplication.isPlaying == false) { // Run through the parents and see if this sprite attached to a clip Transform parentTransform = transform.parent; while (parentTransform != null) { exClipping parentClip = parentTransform.GetComponent <exClipping>(); if (parentClip != null) { SetClip(parentClip); return; } else { exSpriteBase parentSprite = parentTransform.GetComponent <exSpriteBase>(); if (parentSprite != null) { SetClip(parentSprite.clip_); return; } else { parentTransform = parentTransform.parent; } } } // No clip SetClip(null); } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ static void GetSpritesFromPrefabs(string _path, ref List <exSpriteBase> _sprites) { // Process the list of files found in the directory. string [] files = Directory.GetFiles(_path, "*.prefab"); foreach (string fileName in files) { Object prefab = (Object)AssetDatabase.LoadAssetAtPath(fileName, typeof(Object)); if (prefab) { Object [] objs = EditorUtility.CollectDeepHierarchy(new Object [] { prefab }); foreach (Object o in objs) { GameObject go = o as GameObject; if (go != null) { exSpriteBase sp = go.GetComponent <exSpriteBase>(); if (sp != null) { _sprites.Add(sp); } } } } } // Recurse into subdirectories of this directory. string [] dirs = Directory.GetDirectories(_path); foreach (string dirName in dirs) { GetSpritesFromPrefabs(dirName, ref _sprites); } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void OnEnable() { if (sprite == null) { sprite = GetComponent <exSpriteBase>(); } }
public void UnregisterColor(exSpriteBase _sprite) { for (int i = 0; i < colorInfos.Count; ++i) { if (colorInfos[i].sprite == _sprite) { colorInfos.RemoveAt(i); break; } } }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Awake() { sprite = GetComponent <exSpriteBase>(); ppfCamera = sprite.renderCamera.GetComponent <exPixelPerfectCamera>(); if (ppfCamera == null) { ppfCamera = sprite.renderCamera.gameObject.AddComponent <exPixelPerfectCamera>(); } UpdatePixelPerfectCamera(ppfCamera); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void RegisterAllChildren() { colorInfos.Clear(); exSpriteBase[] sprites = GetComponentsInChildren <exSpriteBase>(); for (int i = 0; i < sprites.Length; ++i) { ColorInfo colorInfo = new ColorInfo(); exSpriteBase sprite = sprites[i]; colorInfo.sprite = sprite; colorInfo.color = sprite.color; colorInfos.Add(colorInfo); } }
// ------------------------------------------------------------------ /// \param _sprite the in sprite /// \param _toSprite the vector from camera to sprite // ------------------------------------------------------------------ public void CalculatePixelPerfectScale( exSpriteBase _sprite, Vector3 _toSprite ) { if ( (lastOrthographic && scale <= 0.0f) || ratio <= 0.0f ) CalculateScaleAndRatio (); float s = scale; if ( lastOrthographic == false ) { float angle = Vector3.Angle(transform.forward, _toSprite); float depth = Mathf.Cos( angle * Mathf.Deg2Rad ) * _toSprite.magnitude; s = ratio * depth; } _sprite.ppfScale = new Vector2( s, s ); }
public static void SetTextureInfo(exSpriteBase _sprite, ref exTextureInfo _ti, exTextureInfo _newTi, bool _useTextureOffset, exSpriteType _spriteType) { exTextureInfo old = _ti; _ti = _newTi; if (_newTi != null) { if (_newTi.texture == null) { Debug.LogWarning("invalid textureInfo"); } if (_spriteType == exSpriteType.Tiled) { if (old == null || ReferenceEquals(old, _newTi) || _newTi.width != old.width || _newTi.height != old.height) { (_sprite as exISprite).UpdateBufferSize(); _sprite.updateFlags |= exUpdateFlags.Vertex; // tile数量可能不变,但是间距可能会改变 } } else if (_spriteType == exSpriteType.Diced) { //if (_newTi.isDiced == false) { // Debug.LogWarning ("The texture info is not diced!"); //} (_sprite as exISprite).UpdateBufferSize(); _sprite.updateFlags |= exUpdateFlags.Vertex; } else { if (_sprite.customSize == false && (old == null || (_newTi.width != old.width || _newTi.height != old.height))) { _sprite.updateFlags |= exUpdateFlags.Vertex; } } if (_useTextureOffset) { _sprite.updateFlags |= exUpdateFlags.Vertex; } _sprite.updateFlags |= exUpdateFlags.UV; // 换了texture,UV也会重算,不换texture就更要改UV,否则没有换textureInfo的必要了。 if (old == null || ReferenceEquals(old.texture, _newTi.texture) == false) { // texture changed _sprite.updateFlags |= (exUpdateFlags.Vertex | exUpdateFlags.UV); (_sprite as exISprite).UpdateMaterial(); } } }
// ------------------------------------------------------------------ /// \param _sprite the in sprite /// \param _depthToCamera the depth to camera // ------------------------------------------------------------------ public void CalculatePixelPerfectScale(exSpriteBase _sprite, float _depthToCamera) { if ((camera.orthographic && scale <= 0.0f) || ratio <= 0.0f) { CalculateScaleAndRatio(); } float s = scale; if (camera.orthographic == false) { s = ratio * _depthToCamera; } _sprite.ppfScale = new Vector2(s, s); }
/////////////////////////////////////////////////////////////////////////////// // Overridable functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Awake() { // TODO: NOTE: without GetClippedMaterial, the material table can't store new material, and UpdateClipMaterials will do nothing { exSpriteBase[] spritesToAdd = gameObject.GetComponentsInChildren <exSpriteBase> (true); for (int spriteIndex = 0; spriteIndex < spritesToAdd.Length; ++spriteIndex) { exSpriteBase sprite = spritesToAdd [spriteIndex]; sprite.clip = this; //Material tmp = sprite.material; } // } TODO end currentPos = new Vector2(transform.position.x, transform.position.y); dirty = false; UpdateClipMaterials(); }
// ------------------------------------------------------------------ /// \param _sprite the in sprite /// \param _toSprite the vector from camera to sprite // ------------------------------------------------------------------ public void CalculatePixelPerfectScale(exSpriteBase _sprite, Vector3 _toSprite) { if ((lastOrthographic && scale <= 0.0f) || ratio <= 0.0f) { CalculateScaleAndRatio(); } float s = scale; if (lastOrthographic == false) { float angle = Vector3.Angle(transform.forward, _toSprite); float depth = Mathf.Cos(angle * Mathf.Deg2Rad) * _toSprite.magnitude; s = ratio * depth; } _sprite.ppfScale = new Vector2(s, s); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected new void Update () { if ( spriteBase == null ) { spriteBase = GetComponent<exSpriteBase>(); if ( spriteBase == null ) { Debug.LogError("Can't find exSpriteBase Component in GameObject " + gameObject.name); return; } } if ( lastScale != spriteBase.scale ) { lastScale = spriteBase.scale; spriteBase.updateFlags |= exPlane.UpdateFlags.Vertex; } if ( lastShear != spriteBase.shear ) { lastShear = spriteBase.shear; spriteBase.updateFlags |= exPlane.UpdateFlags.Vertex; } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void RecursivelyNormalizeScale(this Transform _trans) { // foreach (Transform child in _trans) { child.RecursivelyNormalizeScale(); } // if we don't have exSpriteBase component, return exSpriteBase sprite = _trans.GetComponent <exSpriteBase>(); if (sprite == null) { return; } // sprite.scale = new Vector2(sprite.scale.x * _trans.lossyScale.x, sprite.scale.y * _trans.lossyScale.y); // DELME { // // // switch ( sprite.plane ) { // case exSprite.Plane.XY: // sprite.scale = new Vector2( sprite.scale.x * _trans.lossyScale.x, sprite.scale.y * _trans.lossyScale.y ); // break; // case exSprite.Plane.XZ: // sprite.scale = new Vector2( sprite.scale.x * _trans.lossyScale.x, sprite.scale.y * _trans.lossyScale.z ); // break; // case exSprite.Plane.ZY: // sprite.scale = new Vector2( sprite.scale.x * _trans.lossyScale.z, sprite.scale.y * _trans.lossyScale.y ); // break; // } // } DELME end // if (_trans.parent != null) { _trans.localPosition = new Vector3(_trans.localPosition.x * _trans.parent.lossyScale.x, _trans.localPosition.y * _trans.parent.lossyScale.y, _trans.localPosition.z * _trans.parent.lossyScale.z); } _trans.localScale = Vector3.one; }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void SetSize(exSpriteBase _sp, float _newWidth, float _newHeight) { exSprite spriteBG = _sp as exSprite; if (spriteBG) { spriteBG.width = _newWidth; spriteBG.height = _newHeight; } else { exSpriteBorder borderBG = _sp as exSpriteBorder; if (borderBG) { borderBG.width = _newWidth; borderBG.height = _newHeight; } } }
// ------------------------------------------------------------------ // 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); } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ static void GetSpritesFromPrefabsInSelection(ref List <exSpriteBase> _sprites) { Object[] prefabs = Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets); foreach (Object prefab in prefabs) { Object [] objs = EditorUtility.CollectDeepHierarchy(new Object [] { prefab }); foreach (Object o in objs) { GameObject go = o as GameObject; if (go != null) { exSpriteBase sp = go.GetComponent <exSpriteBase>(); if (sp != null) { _sprites.Add(sp); } } } } }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Awake() { lastScreenWidth = Screen.width; lastScreenHeight = Screen.height; lastOrthographicSize = camera.orthographicSize; lastFieldOfView = camera.fieldOfView; lastOrthographic = camera.orthographic; CalculateScaleAndRatio(); exPixelPerfect[] ppfs = GameObject.FindObjectsOfType(typeof(exPixelPerfect)) as exPixelPerfect[]; for (int i = 0; i < ppfs.Length; ++i) { exSpriteBase sprite = ppfs[i].GetComponent <exSpriteBase>(); if (sprite == null || sprite.renderCamera != camera) { continue; } CalculatePixelPerfectScale(sprite, sprite.transform.position - transform.position); sprite.Commit(); } }
public void RegisterColor(exSpriteBase _sprite) { bool founded = false; for (int i = 0; i < colorInfos.Count; ++i) { ColorInfo colorInfo = colorInfos[i]; if (colorInfo.sprite == _sprite) { colorInfo.color = _sprite.color; founded = true; } } if (founded == false) { ColorInfo colorInfo = new ColorInfo(); colorInfo.sprite = _sprite; colorInfo.color = _sprite.color; colorInfos.Add(colorInfo); } }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected new void OnEnable() { base.OnEnable(); if (target != editSpriteBase) { editSpriteBase = target as exSpriteBase; // get collision type if (editSpriteBase.GetComponent <BoxCollider>() != null) { collisionType = CollisionType.Boxed; } else if (editSpriteBase.GetComponent <MeshCollider>() != null) { collisionType = CollisionType.Mesh; } else { collisionType = CollisionType.None; } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected new void LateUpdate() { if (spriteBase == null) { spriteBase = GetComponent <exSpriteBase>(); if (spriteBase == null) { Debug.LogError("Can't find exSpriteBase Component in GameObject " + gameObject.name); return; } } if (lastScale != spriteBase.scale) { lastScale = spriteBase.scale; spriteBase.updateFlags |= exPlane.UpdateFlags.Vertex; } if (lastShear != spriteBase.shear) { lastShear = spriteBase.shear; spriteBase.updateFlags |= exPlane.UpdateFlags.Vertex; } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void AddEffect_Offset(exSpriteBase _target, EffectEventType _type, exEase.Type _curveType, Vector2 _to, float _duration) { exUIControl ctrl = GetComponent <exUIControl>(); if (ctrl) { EffectInfo_Offset info = new EffectInfo_Offset(); info.duration = _duration; info.target = _target; info.normal = _target.offset; info.curveType = _curveType; EffectInfo_Offset.PropInfo propInfo = new EffectInfo_Offset.PropInfo(); propInfo.type = _type; propInfo.val = _to; info.propInfos.Add(propInfo); EffectState_Offset state = new EffectState_Offset(); state.info = info; state.func = info.GetCurveFunction(); AddState_Offset(ctrl, state); } }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Awake() { lastScreenWidth = Screen.width; lastScreenHeight = Screen.height; lastOrthographicSize = camera.orthographicSize; lastFieldOfView = camera.fieldOfView; lastOrthographic = camera.orthographic; CalculateScaleAndRatio(); exPixelPerfect[] ppfs = GameObject.FindObjectsOfType(typeof(exPixelPerfect)) as exPixelPerfect[]; foreach (exPixelPerfect ppf in ppfs) { exSpriteBase sprite = ppf.GetComponent <exSpriteBase>(); if (sprite == null || sprite.renderCamera != camera) { continue; } Vector3 toCamera = sprite.transform.position - transform.position; CalculatePixelPerfectScale(sprite, toCamera.magnitude); sprite.Commit(); } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public new void OnSceneGUI() { exSpriteBase spriteBase = target as exSpriteBase; exEditorUtility.GL_DrawWireFrame(spriteBase, Color.white, false); if (spriteBase && spriteBase.customSize) { Vector3 size; Vector3 center; bool changed = ProcessSceneEditorHandles(out size, out center); if (changed) { //center.z = originalCenterZ; exISprite sprite = spriteBase as exISprite; if (sprite != null) { ApplySpriteScale(sprite, size, center); // also update all planes in the same compnent exPlane[] planes = spriteBase.GetComponents <exPlane>(); for (int i = 0; i < planes.Length; ++i) { exPlane plane = planes[i]; if (plane != this) { // plane.width = sprite.width; // plane.height = sprite.height; plane.anchor = sprite.anchor; plane.offset = sprite.offset; } } } } } }
// ------------------------------------------------------------------ /// Add a sprite to this clip. /// NOTE: You can also use exSpriteBase.SetClip for convenience. // ------------------------------------------------------------------ public void Add(exSpriteBase _sprite) { exClipping oldClip = _sprite.clip; if (ReferenceEquals(oldClip, this)) { return; } if (oldClip != null) { oldClip.Remove(_sprite); } exSpriteBase[] spritesToAdd = _sprite.GetComponentsInChildren <exSpriteBase> (true); for (int spriteIndex = 0; spriteIndex < spritesToAdd.Length; ++spriteIndex) { spritesToAdd [spriteIndex].clip = this; } if (_sprite.transform.IsChildOf(transform) == false) { _sprite.transform.parent = transform; } dirty = true; }
public void RegisterColor( exSpriteBase _sprite ) { bool founded = false; for ( int i = 0; i < colorInfos.Count; ++i ) { ColorInfo colorInfo = colorInfos[i]; if ( colorInfo.sprite == _sprite ) { colorInfo.color = _sprite.color; founded = true; } } if ( founded == false ) { ColorInfo colorInfo = new ColorInfo(); colorInfo.sprite = _sprite; colorInfo.color = _sprite.color; colorInfos.Add(colorInfo); } }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected new void OnEnable() { base.OnEnable(); if ( target != editSpriteBase ) { editSpriteBase = target as exSpriteBase; // get collision type if ( editSpriteBase.GetComponent<BoxCollider>() != null ) { collisionType = CollisionType.Boxed; } else if ( editSpriteBase.GetComponent<MeshCollider>() != null ) { collisionType = CollisionType.Mesh; } else { collisionType = CollisionType.None; } } }
public void DestroyScore(exSpriteBase _score) { _score.enabled = false; scorePool.Return(_score); }
// ------------------------------------------------------------------ // Change vertex buffer from simple to tiled // ------------------------------------------------------------------ public static void TiledUpdateVertexBuffer(exSpriteBase _sprite, exTextureInfo _textureInfo, bool _useTextureOffset, Vector2 _tiledSpacing, Space _space, exList<Vector3> _vertices, int _startIndex) { /* tile index: 8 9 10 11 4 5 6 7 0 1 2 3 */ //if (_vertices.Count == 0) { // return; //} int oriW = _textureInfo.width; int oriH = _textureInfo.height; int oriRawW = _textureInfo.rawWidth; int oriRawH = _textureInfo.rawHeight; try { // use entire sprite size _textureInfo.width = Mathf.Max((int)Mathf.Abs(_sprite.width), 1); _textureInfo.height = Mathf.Max((int)Mathf.Abs(_sprite.height), 1); _textureInfo.rawWidth = Mathf.Max(_textureInfo.width + oriRawW - oriW, 1); _textureInfo.rawHeight = Mathf.Max(_textureInfo.height + oriRawH - oriH, 1); // get entire sprite SimpleUpdateVertexBuffer(_sprite, _textureInfo, _useTextureOffset, _space, _vertices, _startIndex); } finally { // restore _textureInfo.width = oriW; _textureInfo.height = oriH; _textureInfo.rawWidth = oriRawW; _textureInfo.rawHeight = oriRawH; } Vector3 v0 = _vertices.buffer [_startIndex + 0]; Vector3 v1 = _vertices.buffer [_startIndex + 1]; Vector3 v2 = _vertices.buffer [_startIndex + 2]; int colCount, rowCount; exSpriteUtility.GetTilingCount ((exISprite)_sprite, out colCount, out rowCount); Vector2 lastTileRawSize = new Vector2(_sprite.width % (_textureInfo.width + _tiledSpacing.x), _sprite.height % (_textureInfo.height + _tiledSpacing.y)); Vector3 horizontalTileDis, verticalTileDis; if (lastTileRawSize.x > 0.0f) { float perc = lastTileRawSize.x / (_textureInfo.width + _tiledSpacing.x); horizontalTileDis = (v2 - v1) / (colCount - 1 + perc); } else { horizontalTileDis = (v2 - v1) / colCount; } if (lastTileRawSize.y > 0.0f) { float perc = lastTileRawSize.y / (_textureInfo.height + _tiledSpacing.y); verticalTileDis = (v1 - v0) / (rowCount - 1 + perc); } else { verticalTileDis = (v1 - v0) / rowCount; } Vector2 lastTilePercent = new Vector2(lastTileRawSize.x / _textureInfo.width, lastTileRawSize.y / _textureInfo.height); Vector3 trimedTileBottomToTop = verticalTileDis / (_textureInfo.height + _tiledSpacing.y) * _textureInfo.height; Vector3 trimedTileLeftToRight = horizontalTileDis / (_textureInfo.width + _tiledSpacing.x) * _textureInfo.width; int i = _startIndex; Vector3 rowBottomLeft = v0; for (int r = 0; r < rowCount; ++r) { Vector3 bottomLeft = rowBottomLeft; Vector3 topLeft; if (r < rowCount - 1 || lastTilePercent.y >= 1.0f || lastTilePercent.y == 0.0f) { topLeft = bottomLeft + trimedTileBottomToTop; } else { topLeft = v1; // clip last row } for (int c = 0; c < colCount; ++c) { _vertices.buffer[i++] = bottomLeft; _vertices.buffer[i++] = topLeft; _vertices.buffer[i++] = topLeft + trimedTileLeftToRight; _vertices.buffer[i++] = bottomLeft + trimedTileLeftToRight; // next column bottomLeft += horizontalTileDis; topLeft += horizontalTileDis; } // clip last column if (0.0f < lastTilePercent.x && lastTilePercent.x < 1.0f) { Vector3 clipped = trimedTileLeftToRight * (1.0f - lastTilePercent.x); _vertices.buffer[i - 2] -= clipped; _vertices.buffer[i - 1] -= clipped; } // next row rowBottomLeft += verticalTileDis; } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void SimpleUpdateBuffers(exSpriteBase _sprite, exTextureInfo _textureInfo, bool _useTextureOffset, Space _space, exList<Vector3> _vertices, exList<Vector2> _uvs, exList<int> _indices, int _vbIndex, int _ibIndex) { if (/*transparent_ == false && */(_sprite.updateFlags & exUpdateFlags.Vertex) != 0) { SpriteBuilder.SimpleUpdateVertexBuffer(_sprite, _textureInfo, _useTextureOffset, _space, _vertices, _vbIndex); } if (/*transparent_ == false && */(_sprite.updateFlags & exUpdateFlags.Index) != 0 && _indices != null) { _indices.buffer[_ibIndex] = _vbIndex; _indices.buffer[_ibIndex + 1] = _vbIndex + 1; _indices.buffer[_ibIndex + 2] = _vbIndex + 2; _indices.buffer[_ibIndex + 3] = _vbIndex + 2; _indices.buffer[_ibIndex + 4] = _vbIndex + 3; _indices.buffer[_ibIndex + 5] = _vbIndex; } if (/*transparent_ == false && */(_sprite.updateFlags & exUpdateFlags.UV) != 0) { Vector2 texelSize; if (_textureInfo.texture != null) { texelSize = _textureInfo.texture.texelSize; } else { texelSize = new Vector2(1.0f / _textureInfo.rawWidth, 1.0f / _textureInfo.rawHeight); } Vector2 start = new Vector2((float)_textureInfo.x * texelSize.x, (float)_textureInfo.y * texelSize.y); Vector2 end = new Vector2((float)(_textureInfo.x + _textureInfo.rotatedWidth) * texelSize.x, (float)(_textureInfo.y + _textureInfo.rotatedHeight) * texelSize.y); if ( _textureInfo.rotated ) { _uvs.buffer[_vbIndex + 0] = new Vector2(end.x, start.y); _uvs.buffer[_vbIndex + 1] = start; _uvs.buffer[_vbIndex + 2] = new Vector2(start.x, end.y); _uvs.buffer[_vbIndex + 3] = end; } else { _uvs.buffer[_vbIndex + 0] = start; _uvs.buffer[_vbIndex + 1] = new Vector2(start.x, end.y); _uvs.buffer[_vbIndex + 2] = end; _uvs.buffer[_vbIndex + 3] = new Vector2(end.x, start.y); } } }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected override void Awake() { spriteBase = GetComponent<exSpriteBase>(); lastScale = spriteBase.scale; lastShear = spriteBase.shear; }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void OnPreRender() { bool changed = false; // check if we need to update pixel perfect scale if (customResolution) { if (lastScreenWidth != width || lastScreenHeight != height) { lastScreenWidth = width; lastScreenHeight = height; changed = true; } } else { if (lastScreenWidth != Screen.width || lastScreenHeight != Screen.height) { lastScreenWidth = Screen.width; lastScreenHeight = Screen.height; changed = true; } } // if (camera.orthographic) { if (fixOrthographicSize) { if (lastOrthographicSize != orthographicSize) { lastOrthographicSize = orthographicSize; changed = true; } } else { if (lastOrthographicSize != camera.orthographicSize) { lastOrthographicSize = camera.orthographicSize; changed = true; } } } else { if (lastFieldOfView != camera.fieldOfView) { lastFieldOfView = camera.fieldOfView; changed = true; } } // if (lastOrthographic != camera.orthographic) { lastOrthographic = camera.orthographic; changed = true; } // if (changed) { CalculateScaleAndRatio(); exPixelPerfect[] ppfs = GameObject.FindObjectsOfType(typeof(exPixelPerfect)) as exPixelPerfect[]; for (int i = 0; i < ppfs.Length; ++i) { exSpriteBase sprite = ppfs[i].GetComponent <exSpriteBase>(); if (sprite == null || sprite.renderCamera != camera) { continue; } CalculatePixelPerfectScale(sprite, sprite.transform.position - transform.position); sprite.Commit(); } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void OnEnable () { if ( sprite == null ) { sprite = GetComponent<exSpriteBase>(); } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected override void DoInspectorGUI() { base.DoInspectorGUI(); // if settingsStyles is null if (styles == null) { styles = new Styles(); } // GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Sync Size", GUILayout.Width(70), GUILayout.Height(20))) { 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); } } } GUILayout.EndHorizontal(); // EditorGUILayout.Space(); EditorGUILayout.PropertyField(priorityProp); // active EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(activeProp, new GUIContent("Active")); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { exUIControl ctrl = obj as exUIControl; if (ctrl) { ctrl.activeSelf = activeProp.boolValue; EditorUtility.SetDirty(ctrl); } } } // grabMouseOrTouch EditorGUILayout.PropertyField(grabMouseOrTouchProp, new GUIContent("Grab Mouse Or Touch")); // use collider EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(useColliderProp, new GUIContent("Use Collider")); if (EditorGUI.EndChangeCheck()) { if (useColliderProp.boolValue) { foreach (Object obj in serializedObject.targetObjects) { exUIControl ctrl = obj as exUIControl; if (ctrl) { Collider collider = ctrl.GetComponent <Collider>(); if (collider == null) { collider = ctrl.gameObject.AddComponent <BoxCollider>(); } BoxCollider boxCollider = collider as BoxCollider; if (boxCollider != null) { Rect localRect = ctrl.GetLocalAABoundingRect(); boxCollider.center = new Vector3(localRect.center.x, localRect.center.y, boxCollider.center.z); boxCollider.size = new Vector3(localRect.width, localRect.height, boxCollider.size.z); } } } } else { foreach (Object obj in serializedObject.targetObjects) { exUIControl ctrl = obj as exUIControl; if (ctrl) { Collider[] colliders = ctrl.GetComponents <Collider>(); for (int i = 0; i < colliders.Length; ++i) { Object.DestroyImmediate(colliders[i]); } } } } } if (useColliderProp.boolValue) { EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Sync Collider", GUILayout.MinWidth(50), GUILayout.Height(20))) { foreach (Object obj in serializedObject.targetObjects) { exUIControl ctrl = obj as exUIControl; if (ctrl) { BoxCollider boxCollider = ctrl.GetComponent <BoxCollider>(); Rect localRect = ctrl.GetLocalAABoundingRect(); boxCollider.center = new Vector3(localRect.center.x, localRect.center.y, boxCollider.center.z); boxCollider.size = new Vector3(localRect.width, localRect.height, boxCollider.size.z); } } } EditorGUILayout.EndHorizontal(); } if (serializedObject.isEditingMultipleObjects == false) { exUIControl uiControl = target as exUIControl; // event adding selector List <string> eventDefNameList = new List <string>(); eventDefNameList.Add("Event List"); eventDefNameList.AddRange(uiControl.GetEventNames()); foreach (exUIControl.EventTrigger eventTrigger in uiControl.events) { int idx = eventDefNameList.IndexOf(eventTrigger.name); if (idx != -1) { eventDefNameList.RemoveAt(idx); } } int choice = EditorGUILayout.Popup("Add Event", 0, eventDefNameList.ToArray()); if (choice != 0) { exUIControl.EventTrigger newTrigger = new exUIControl.EventTrigger(eventDefNameList[choice]); uiControl.events.Add(newTrigger); EditorUtility.SetDirty(target); } // event triggers for (int i = 0; i < uiControl.events.Count; ++i) { EditorGUILayout.Space(); exUIControl.EventTrigger eventTrigger = uiControl.events[i]; if (EventField(eventTrigger)) { uiControl.events.RemoveAt(i); --i; EditorUtility.SetDirty(target); } EditorGUILayout.Space(); } } EditorGUILayout.Space(); }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Awake () { sprite = GetComponent<exSpriteBase>(); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void Return( exSpriteBase _score ) { ++idx; scores[idx] = _score; }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected override void DoInspectorGUI() { // customSize EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(customSizeProp, new GUIContent("Custom Size")); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { exSpriteBase sp = obj as exSpriteBase; if (sp) { sp.customSize = customSizeProp.boolValue; EditorUtility.SetDirty(sp); } } } // if customSize == true EditorGUI.indentLevel++; if (customSizeProp.boolValue) { // width EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(widthProp, new GUIContent("Width")); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { exSpriteBase sp = obj as exSpriteBase; if (sp) { sp.width = Mathf.Max(widthProp.floatValue, 0f); EditorUtility.SetDirty(sp); } } } // height EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(heightProp, new GUIContent("Height")); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { exSpriteBase sp = obj as exSpriteBase; if (sp) { sp.height = Mathf.Max(heightProp.floatValue, 0f); EditorUtility.SetDirty(sp); } } } } // if customSize == false else { GUI.enabled = false; if (serializedObject.isEditingMultipleObjects == false) { exSpriteBase spriteBase = serializedObject.targetObject as exSpriteBase; EditorGUILayout.FloatField(new GUIContent("Width"), spriteBase.width); EditorGUILayout.FloatField(new GUIContent("Height"), spriteBase.height); } GUI.enabled = true; } EditorGUI.indentLevel--; // anchor EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(anchorProp, new GUIContent("Anchor")); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { exSpriteBase sp = obj as exSpriteBase; if (sp) { sp.anchor = (Anchor)anchorProp.enumValueIndex; EditorUtility.SetDirty(sp); } } } // offset EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(offsetProp, new GUIContent("Offset"), true); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { exSpriteBase sp = obj as exSpriteBase; if (sp) { sp.offset = offsetProp.vector2Value; EditorUtility.SetDirty(sp); } } } // shear EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(shearProp, new GUIContent("Shear"), true); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { exSpriteBase sp = obj as exSpriteBase; if (sp) { sp.shear = shearProp.vector2Value; EditorUtility.SetDirty(sp); } } } // color EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(colorProp, new GUIContent("Color"), true); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { exSpriteBase sp = obj as exSpriteBase; if (sp) { sp.color = colorProp.colorValue; EditorUtility.SetDirty(sp); } } } // shader EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(shaderProp, new GUIContent("Shader")); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { exSpriteBase sp = obj as exSpriteBase; if (sp) { sp.shader = shaderProp.objectReferenceValue as Shader; EditorUtility.SetDirty(sp); } } } EditorGUILayout.Space(); }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected new void Awake() { spriteBase = GetComponent <exSpriteBase>(); lastScale = spriteBase.scale; lastShear = spriteBase.shear; }
// ------------------------------------------------------------------ // Change vertex buffer from simple to sliced // ------------------------------------------------------------------ public static void SimpleVertexBufferToSliced(exSpriteBase _sprite, exTextureInfo textureInfo_, exList<Vector3> _vertices, int _startIndex) { /* vertex index: 12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3 */ // get border size float leftBorderSize, rightBorderSize, topBorderSize, bottomBorderSize; exISprite iSprite = _sprite as exISprite; if (iSprite.customBorderSize) { leftBorderSize = iSprite.leftBorderSize; rightBorderSize = iSprite.rightBorderSize; topBorderSize = iSprite.topBorderSize; bottomBorderSize = iSprite.bottomBorderSize; } else { leftBorderSize = (float)textureInfo_.borderLeft; rightBorderSize = (float)textureInfo_.borderRight; topBorderSize = (float)textureInfo_.borderTop; bottomBorderSize = (float)textureInfo_.borderBottom; } // left right columns Vector3 v0 = _vertices.buffer[_startIndex + 0]; Vector3 v12 = _vertices.buffer[_startIndex + 1]; Vector3 v15 = _vertices.buffer[_startIndex + 2]; Vector3 v3 = _vertices.buffer[_startIndex + 3]; //_vertices.buffer[_startIndex + 0] = v0; //_vertices.buffer[_startIndex + 3] = v3; _vertices.buffer[_startIndex + 12] = v12; _vertices.buffer[_startIndex + 15] = v15; float height = _sprite.height; float yStep1 = bottomBorderSize / height; // position step, not uv step float yStep2 = (height - topBorderSize) / height; _vertices.buffer[_startIndex + 4] = v0 + (v12 - v0) * yStep1; _vertices.buffer[_startIndex + 7] = v3 + (v15 - v3) * yStep1; _vertices.buffer[_startIndex + 8] = v0 + (v12 - v0) * yStep2; _vertices.buffer[_startIndex + 11] = v3 + (v15 - v3) * yStep2; // mid columns float width = _sprite.width; float xStep1 = leftBorderSize / width; float xStep2 = (width - rightBorderSize) / width; for (int i = 0; i <= 12; i += 4) { Vector3 left = _vertices.buffer[_startIndex + i]; Vector3 right = _vertices.buffer[_startIndex + i + 3]; _vertices.buffer[_startIndex + i + 1] = left + (right - left) * xStep1; _vertices.buffer[_startIndex + i + 2] = left + (right - left) * xStep2; } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void Remove(exSpriteBase _sprite) { Remove(_sprite.gameObject); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void SimpleVertexBufferToDiced(exSpriteBase _sprite, exTextureInfo _textureInfo, exList<Vector3> _vertices, int _startIndex) { /* dice index: 8 9 10 11 4 5 6 7 0 1 2 3 */ if (_textureInfo.isDiced == false) { return; } Vector3 v0 = _vertices.buffer [_startIndex + 0]; Vector3 v1 = _vertices.buffer [_startIndex + 1]; Vector3 v2 = _vertices.buffer [_startIndex + 2]; int colCount, rowCount; exSpriteUtility.GetDicingCount (_textureInfo, out colCount, out rowCount); Vector2 lastTileRawSize = new Vector2(); if (_textureInfo.diceUnitWidth > 0) { lastTileRawSize.x = _textureInfo.width % _textureInfo.diceUnitWidth; } if (_textureInfo.diceUnitHeight > 0) { lastTileRawSize.y = _textureInfo.height % _textureInfo.diceUnitHeight; } Vector3 diceLeftToRight, diceBottomToTop; if (lastTileRawSize.x > 0.0f) { float perc = lastTileRawSize.x / _textureInfo.diceUnitWidth; diceLeftToRight = (v2 - v1) / (colCount - 1 + perc); } else { diceLeftToRight = (v2 - v1) / colCount; } if (lastTileRawSize.y > 0.0f) { float perc = lastTileRawSize.y / _textureInfo.diceUnitHeight; diceBottomToTop = (v1 - v0) / (rowCount - 1 + perc); } else { diceBottomToTop = (v1 - v0) / rowCount; } Vector3 l2rStepPerTile = diceLeftToRight / _textureInfo.diceUnitWidth; Vector3 b2tStepPerTile = diceBottomToTop / _textureInfo.diceUnitHeight; int i = _startIndex; Vector3 rowBottomLeft = v0; DiceEnumerator diceEnumerator = _textureInfo.dices; for (int r = 0; r < rowCount; ++r) { Vector3 bottomLeft = rowBottomLeft; Vector3 topLeft = bottomLeft + diceBottomToTop; for (int c = 0; c < colCount; ++c) { bool hasNext = diceEnumerator.MoveNext (); if (hasNext == false) { // 后面都被Trim掉了 return; } exTextureInfo.Dice dice = diceEnumerator.Current; if (dice.sizeType == exTextureInfo.DiceType.Max) { _vertices.buffer[i++] = bottomLeft; _vertices.buffer[i++] = topLeft; _vertices.buffer[i++] = topLeft + diceLeftToRight; _vertices.buffer[i++] = bottomLeft + diceLeftToRight; } else if (dice.sizeType == exTextureInfo.DiceType.Trimmed) { Vector3 offsetX = l2rStepPerTile * dice.offset_x; Vector3 offsetY = b2tStepPerTile * dice.offset_y; Vector3 offsetEndX = l2rStepPerTile * (dice.offset_x + dice.width); Vector3 offsetEndY = b2tStepPerTile * (dice.offset_y + dice.height); _vertices.buffer[i++] = bottomLeft + offsetX + offsetY; _vertices.buffer[i++] = bottomLeft + offsetX + offsetEndY; _vertices.buffer[i++] = bottomLeft + offsetEndX + offsetEndY; _vertices.buffer[i++] = bottomLeft + offsetEndX + offsetY; } bottomLeft += diceLeftToRight; // next column topLeft += diceLeftToRight; // next column } // next row rowBottomLeft += diceBottomToTop; } exDebug.Assert(diceEnumerator.MoveNext() == false, string.Format("row: {0} col: {1} ", rowCount, colCount)); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void TiledUpdateBuffers(exSpriteBase _sprite, exTextureInfo _textureInfo, bool _useTextureOffset, Vector2 _tiledSpacing, Space _space, exList<Vector3> _vertices, exList<Vector2> _uvs, exList<int> _indices, int _vbIndex, int _ibIndex) { if (_vertices.Count == 0) { return; } SpriteBuilder.SimpleUpdateBuffers(_sprite, _textureInfo, _useTextureOffset, _space, _vertices, _uvs, _indices, _vbIndex, _ibIndex); if ((_sprite.updateFlags & exUpdateFlags.Vertex) != 0) { TiledUpdateVertexBuffer(_sprite, _textureInfo, _useTextureOffset, _tiledSpacing, _space, _vertices, _vbIndex); } int colCount, rowCount; exSpriteUtility.GetTilingCount ((exISprite)_sprite, out colCount, out rowCount); if ((_sprite.updateFlags & exUpdateFlags.Index) != 0 && _indices != null) { /* tile index: 8 9 10 11 4 5 6 7 0 1 2 3 */ int v = _vbIndex; int i = _ibIndex; int quadCount = colCount * rowCount; for (int q = 0; q < quadCount; ++q) { _indices.buffer[i++] = v; _indices.buffer[i++] = v + 1; _indices.buffer[i++] = v + 2; _indices.buffer[i++] = v + 2; _indices.buffer[i++] = v + 3; _indices.buffer[i++] = v; v += exMesh.QUAD_VERTEX_COUNT; } } if ((_sprite.updateFlags & exUpdateFlags.UV) != 0) { Vector2 uv0 = _uvs.buffer[_vbIndex + 0]; Vector2 uv2 = _uvs.buffer[_vbIndex + 2]; Vector2 uv3 = _uvs.buffer[_vbIndex + 3]; Vector2 lastTileRawSize = new Vector2(_sprite.width % (_textureInfo.width + _tiledSpacing.x), _sprite.height % (_textureInfo.height + _tiledSpacing.y)); Vector2 clippedUv2 = uv2; if (0.0f < lastTileRawSize.y && lastTileRawSize.y < _textureInfo.height) { // clipped last row float stepY = lastTileRawSize.y / _textureInfo.height; if (_textureInfo.rotated == false) clippedUv2.y = Mathf.Lerp(uv0.y, uv2.y, stepY); else clippedUv2.x = Mathf.Lerp(uv0.x, uv2.x, stepY); } if (0.0f < lastTileRawSize.x && lastTileRawSize.x < _textureInfo.width) { // clipped last column float stepX = lastTileRawSize.x / _textureInfo.width; if (_textureInfo.rotated == false) clippedUv2.x = Mathf.Lerp(uv0.x, uv2.x, stepX); else clippedUv2.y = Mathf.Lerp(uv0.y, uv2.y, stepX); } int i = _vbIndex; if (_textureInfo.rotated == false) { for (int r = 0; r < rowCount; ++r) { float rowTopUv = (r < rowCount - 1) ? uv2.y : clippedUv2.y; for (int c = 0; c < colCount; ++c) { _uvs.buffer[i++] = uv0; _uvs.buffer[i++] = new Vector2(uv0.x, rowTopUv); _uvs.buffer[i++] = new Vector2(uv2.x, rowTopUv); _uvs.buffer[i++] = uv3; } // clip last column _uvs.buffer[i - 2].x = clippedUv2.x; _uvs.buffer[i - 1].x = clippedUv2.x; } } else { for (int r = 0; r < rowCount; ++r) { float rowTopUv = (r < rowCount - 1) ? uv2.x : clippedUv2.x; for (int c = 0; c < colCount; ++c) { _uvs.buffer[i++] = uv0; _uvs.buffer[i++] = new Vector2(rowTopUv, uv0.y); _uvs.buffer[i++] = new Vector2(rowTopUv, uv2.y); _uvs.buffer[i++] = uv3; } // clip last column _uvs.buffer[i - 2].y = clippedUv2.y; _uvs.buffer[i - 1].y = clippedUv2.y; } } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void SimpleUpdateVertexBuffer(exSpriteBase _sprite, exTextureInfo _textureInfo, bool _useTextureOffset, Space _space, exList<Vector3> _vertices, int _startIndex) { Vector2 anchorOffset; float halfHeight = _textureInfo.height * 0.5f; float halfWidth = _textureInfo.width * 0.5f; if (_useTextureOffset) { switch (_sprite.anchor) { case Anchor.TopLeft: anchorOffset.x = halfWidth + _textureInfo.trim_x; anchorOffset.y = -halfHeight + _textureInfo.trim_y - (_textureInfo.rawHeight - _textureInfo.height); break; case Anchor.TopCenter: anchorOffset.x = _textureInfo.trim_x - (_textureInfo.rawWidth - _textureInfo.width) * 0.5f; anchorOffset.y = -halfHeight + _textureInfo.trim_y - (_textureInfo.rawHeight - _textureInfo.height); break; case Anchor.TopRight: anchorOffset.x = -halfWidth + _textureInfo.trim_x - (_textureInfo.rawWidth - _textureInfo.width); anchorOffset.y = -halfHeight + _textureInfo.trim_y - (_textureInfo.rawHeight - _textureInfo.height); break; // case Anchor.MidLeft: anchorOffset.x = halfWidth + _textureInfo.trim_x; anchorOffset.y = _textureInfo.trim_y - (_textureInfo.rawHeight - _textureInfo.height) * 0.5f; break; case Anchor.MidCenter: anchorOffset.x = _textureInfo.trim_x - (_textureInfo.rawWidth - _textureInfo.width) * 0.5f; anchorOffset.y = _textureInfo.trim_y - (_textureInfo.rawHeight - _textureInfo.height) * 0.5f; break; case Anchor.MidRight: anchorOffset.x = -halfWidth + _textureInfo.trim_x - (_textureInfo.rawWidth - _textureInfo.width); anchorOffset.y = _textureInfo.trim_y - (_textureInfo.rawHeight - _textureInfo.height) * 0.5f; break; // case Anchor.BotLeft: anchorOffset.x = halfWidth + _textureInfo.trim_x; anchorOffset.y = halfHeight + _textureInfo.trim_y; break; case Anchor.BotCenter: anchorOffset.x = _textureInfo.trim_x - (_textureInfo.rawWidth - _textureInfo.width) * 0.5f; anchorOffset.y = halfHeight + _textureInfo.trim_y; break; case Anchor.BotRight: anchorOffset.x = -halfWidth + _textureInfo.trim_x - (_textureInfo.rawWidth - _textureInfo.width); anchorOffset.y = halfHeight + _textureInfo.trim_y; break; // default: anchorOffset.x = _textureInfo.trim_x - (_textureInfo.rawWidth - _textureInfo.width) * 0.5f; anchorOffset.y = _textureInfo.trim_y - (_textureInfo.rawHeight - _textureInfo.height) * 0.5f; break; } } else { switch ( _sprite.anchor ) { case Anchor.TopLeft : anchorOffset.x = halfWidth; anchorOffset.y = -halfHeight; break; case Anchor.TopCenter : anchorOffset.x = 0.0f; anchorOffset.y = -halfHeight; break; case Anchor.TopRight : anchorOffset.x = -halfWidth; anchorOffset.y = -halfHeight; break; case Anchor.MidLeft : anchorOffset.x = halfWidth; anchorOffset.y = 0.0f; break; case Anchor.MidCenter : anchorOffset.x = 0.0f; anchorOffset.y = 0.0f; break; case Anchor.MidRight : anchorOffset.x = -halfWidth; anchorOffset.y = 0.0f; break; case Anchor.BotLeft : anchorOffset.x = halfWidth; anchorOffset.y = halfHeight; break; case Anchor.BotCenter : anchorOffset.x = 0.0f; anchorOffset.y = halfHeight; break; case Anchor.BotRight : anchorOffset.x = -halfWidth; anchorOffset.y = halfHeight; break; default : anchorOffset.x = 0.0f; anchorOffset.y = 0.0f; break; } } //v1 v2 //v0 v3 Vector3 v0 = new Vector3 (-halfWidth + anchorOffset.x, -halfHeight + anchorOffset.y, 0.0f); Vector3 v1 = new Vector3 (-halfWidth + anchorOffset.x, halfHeight + anchorOffset.y, 0.0f); Vector3 v2 = new Vector3 (halfWidth + anchorOffset.x, halfHeight + anchorOffset.y, 0.0f); Vector3 v3 = new Vector3 (halfWidth + anchorOffset.x, -halfHeight + anchorOffset.y, 0.0f); if (_sprite.customSize) { Vector2 customSizeScale = new Vector2 (_sprite.width / _textureInfo.width, _sprite.height / _textureInfo.height); v0.x *= customSizeScale.x; v0.y *= customSizeScale.y; v1.x *= customSizeScale.x; v1.y *= customSizeScale.y; v2.x *= customSizeScale.x; v2.y *= customSizeScale.y; v3.x *= customSizeScale.x; v3.y *= customSizeScale.y; } Vector3 offset = _sprite.offset; v0 += offset; v1 += offset; v2 += offset; v3 += offset; Vector2 shear = _sprite.shear; if (shear.x != 0) { float offsetX = _sprite.GetScaleY(_space) * shear.x; float topOffset = offsetX * (halfHeight + anchorOffset.y); float botOffset = offsetX * (-halfHeight + anchorOffset.y); v0.x += botOffset; v1.x += topOffset; v2.x += topOffset; v3.x += botOffset; } if (shear.y != 0) { float offsetY = _sprite.GetScaleX(_space) * shear.y; float leftOffset = offsetY * (-halfWidth + anchorOffset.x); float rightOffset = offsetY * (halfWidth + anchorOffset.x); v0.y += leftOffset; v1.y += leftOffset; v2.y += rightOffset; v3.y += rightOffset; } if (_space == Space.World) { exDebug.Assert((_sprite as exLayeredSprite) != null); v0 = _sprite.cachedWorldMatrix.MultiplyPoint3x4(v0); v1 = _sprite.cachedWorldMatrix.MultiplyPoint3x4(v1); v2 = _sprite.cachedWorldMatrix.MultiplyPoint3x4(v2); v3 = _sprite.cachedWorldMatrix.MultiplyPoint3x4(v3); // 将z都设为0,使mesh所有mesh的厚度都为0,这样在mesh进行深度排序时会方便一些。但是不能用于3D Sprite v0.z = 0; v1.z = 0; v2.z = 0; v3.z = 0; } _vertices.buffer[_startIndex + 0] = v0; _vertices.buffer[_startIndex + 1] = v1; _vertices.buffer[_startIndex + 2] = v2; _vertices.buffer[_startIndex + 3] = v3; // TODO: pixel-perfect }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void SlicedUpdateBuffers(exSpriteBase _sprite, exTextureInfo _textureInfo, bool _useTextureOffset, Space _space, exList<Vector3> _vertices, exList<Vector2> _uvs, exList<int> _indices, int _vbIndex, int _ibIndex) { SpriteBuilder.SimpleUpdateBuffers(_sprite, _textureInfo, _useTextureOffset, _space, _vertices, _uvs, _indices, _vbIndex, _ibIndex); if ((_sprite.updateFlags & exUpdateFlags.Vertex) != 0) { SpriteBuilder.SimpleVertexBufferToSliced(_sprite, _textureInfo, _vertices, _vbIndex); } if ((_sprite.updateFlags & exUpdateFlags.Index) != 0 && _indices != null) { bool borderOnly = (_sprite as exISprite).borderOnly; int centerIndexIfBorderOnly = borderOnly ? 5 : int.MinValue; for (int i = 0; i <= 10; ++i) { if (i != 3 && i != 7 && i != centerIndexIfBorderOnly) { // 0 1 2 4 5 6 8 9 10 int blVertexIndex = _vbIndex + i; // bottom left vertex index _indices.buffer[_ibIndex++] = blVertexIndex; _indices.buffer[_ibIndex++] = blVertexIndex + 4; _indices.buffer[_ibIndex++] = blVertexIndex + 5; _indices.buffer[_ibIndex++] = blVertexIndex + 5; _indices.buffer[_ibIndex++] = blVertexIndex + 1; _indices.buffer[_ibIndex++] = blVertexIndex; } } } if ((_sprite.updateFlags & exUpdateFlags.UV) != 0) { float xStep1, xStep2, yStep1, yStep2; if (_textureInfo.rotated == false) { yStep1 = (float)_textureInfo.borderBottom / _textureInfo.height; // uv step, not position step yStep2 = (float)(_textureInfo.height - _textureInfo.borderTop) / _textureInfo.height; xStep1 = (float)_textureInfo.borderLeft / _textureInfo.width; xStep2 = (float)(_textureInfo.width - _textureInfo.borderRight) / _textureInfo.width; } else { xStep1 = (float)_textureInfo.borderBottom / _textureInfo.height; // uv step, not position step xStep2 = (float)(_textureInfo.height - _textureInfo.borderTop) / _textureInfo.height; yStep1 = (float)_textureInfo.borderLeft / _textureInfo.width; yStep2 = (float)(_textureInfo.width - _textureInfo.borderRight) / _textureInfo.width; } Vector2 uv0, uv15; uv0 = _uvs.buffer[_vbIndex + 0]; uv15 = _uvs.buffer[_vbIndex + 2]; Vector2 uv5 = new Vector2(uv0.x + (uv15.x - uv0.x) * xStep1, uv0.y + (uv15.y - uv0.y) * yStep1); Vector2 uv10 = new Vector2(uv0.x + (uv15.x - uv0.x) * xStep2, uv0.y + (uv15.y - uv0.y) * yStep2); if (_textureInfo.rotated == false) { //_uvs.buffer[vertexBufferIndex + 0] = uv0; _uvs.buffer[_vbIndex + 1] = new Vector2(uv5.x, uv0.y); _uvs.buffer[_vbIndex + 2] = new Vector2(uv10.x, uv0.y); _uvs.buffer[_vbIndex + 3] = new Vector2(uv15.x, uv0.y); _uvs.buffer[_vbIndex + 4] = new Vector2(uv0.x, uv5.y); _uvs.buffer[_vbIndex + 5] = uv5; _uvs.buffer[_vbIndex + 6] = new Vector2(uv10.x, uv5.y); _uvs.buffer[_vbIndex + 7] = new Vector2(uv15.x, uv5.y); _uvs.buffer[_vbIndex + 8] = new Vector2(uv0.x, uv10.y); _uvs.buffer[_vbIndex + 9] = new Vector2(uv5.x, uv10.y); _uvs.buffer[_vbIndex + 10] = uv10; _uvs.buffer[_vbIndex + 11] = new Vector2(uv15.x, uv10.y); _uvs.buffer[_vbIndex + 12] = new Vector2(uv0.x, uv15.y); _uvs.buffer[_vbIndex + 13] = new Vector2(uv5.x, uv15.y); _uvs.buffer[_vbIndex + 14] = new Vector2(uv10.x, uv15.y); _uvs.buffer[_vbIndex + 15] = uv15; } else { //_uvs.buffer[vertexBufferIndex + 0] = uv0; _uvs.buffer[_vbIndex + 1] = new Vector2(uv0.x, uv5.y); _uvs.buffer[_vbIndex + 2] = new Vector2(uv0.x, uv10.y); _uvs.buffer[_vbIndex + 3] = new Vector2(uv0.x, uv15.y); _uvs.buffer[_vbIndex + 4] = new Vector2(uv5.x, uv0.y); _uvs.buffer[_vbIndex + 5] = uv5; _uvs.buffer[_vbIndex + 6] = new Vector2(uv5.x, uv10.y); _uvs.buffer[_vbIndex + 7] = new Vector2(uv5.x, uv15.y); _uvs.buffer[_vbIndex + 8] = new Vector2(uv10.x, uv0.y); _uvs.buffer[_vbIndex + 9] = new Vector2(uv10.x, uv5.y); _uvs.buffer[_vbIndex + 10] = uv10; _uvs.buffer[_vbIndex + 11] = new Vector2(uv10.x, uv15.y); _uvs.buffer[_vbIndex + 12] = new Vector2(uv15.x, uv0.y); _uvs.buffer[_vbIndex + 13] = new Vector2(uv15.x, uv5.y); _uvs.buffer[_vbIndex + 14] = new Vector2(uv15.x, uv10.y); _uvs.buffer[_vbIndex + 15] = uv15; } } }
// ------------------------------------------------------------------ /// Add a sprite to this clip. /// NOTE: You can also use exSpriteBase.SetClip for convenience. // ------------------------------------------------------------------ public void Add(exSpriteBase _sprite) { exClipping oldClip = _sprite.clip; if (ReferenceEquals (oldClip, this)) { return; } if (oldClip != null) { oldClip.Remove (_sprite); } exSpriteBase[] spritesToAdd = _sprite.GetComponentsInChildren<exSpriteBase> (true); for (int spriteIndex = 0; spriteIndex < spritesToAdd.Length; ++spriteIndex) { spritesToAdd [spriteIndex].clip = this; } if (_sprite.transform.IsChildOf (transform) == false) { _sprite.transform.parent = transform; } dirty = true; }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected void OffsetInfoField(List <EffectInfo_Offset> _infos) { GUILayout.BeginHorizontal(); GUILayout.Space(4f); GUILayout.BeginVertical(); // name GUILayout.Toggle(true, "Offset", "dragtab"); EditorGUILayout.BeginHorizontal("AS TextArea", GUILayout.MinHeight(10f)); GUILayout.BeginVertical(); // infos for (int i = 0; i < _infos.Count; ++i) { bool infoDeleted = false; EffectInfo_Offset info = _infos[i]; // target EditorGUILayout.BeginHorizontal(); // receiver EditorGUI.BeginChangeCheck(); info.target = EditorGUILayout.ObjectField(info.target, typeof(exSpriteBase), true) as exSpriteBase; if (EditorGUI.EndChangeCheck()) { EditorUtility.SetDirty(target); } // if (info.target != null) { } else { infoDeleted = true; } // Delete if (GUILayout.Button(styles.iconToolbarMinus, "InvisibleButton", GUILayout.Width(20f))) { infoDeleted = true; } GUILayout.Space(3f); EditorGUILayout.EndHorizontal(); // curve, duration EditorGUILayout.BeginHorizontal(); GUILayout.Space(30); EditorGUI.BeginChangeCheck(); GUILayout.Label("Custom Curve"); info.customCurve = EditorGUILayout.Toggle(info.customCurve, GUILayout.Width(15)); if (info.customCurve) { info.curve = EditorGUILayout.CurveField(info.curve); } else { info.curveType = (exEase.Type)EditorGUILayout.EnumPopup(info.curveType); } info.duration = EditorGUILayout.FloatField(info.duration); if (EditorGUI.EndChangeCheck()) { EditorUtility.SetDirty(target); } EditorGUILayout.EndHorizontal(); float x = 0.0f; float y = 0.0f; // normal EditorGUILayout.BeginHorizontal(); GUILayout.Space(30); if (GUILayout.Button("Sync", GUILayout.Width(50))) { info.normal = info.target.offset; EditorUtility.SetDirty(target); } EditorGUI.BeginChangeCheck(); GUILayout.Label("Normal"); GUILayout.Label("X", GUILayout.Width(10)); x = EditorGUILayout.FloatField("", info.normal.x, GUILayout.Width(30)); GUILayout.Label("Y", GUILayout.Width(10)); y = EditorGUILayout.FloatField("", info.normal.y, GUILayout.Width(30)); if (EditorGUI.EndChangeCheck()) { info.normal = new Vector2(x, y); EditorUtility.SetDirty(target); } EditorGUILayout.EndHorizontal(); // Properties for (int j = 0; j < info.propInfos.Count; ++j) { EffectInfo_Offset.PropInfo propInfo = info.propInfos[j]; EditorGUILayout.BeginHorizontal(); GUILayout.Space(30); // propInfoDeleted bool propInfoDeleted = false; if (GUILayout.Button(styles.iconToolbarMinus, "InvisibleButton", GUILayout.Width(styles.iconToolbarMinus.width), GUILayout.Height(styles.iconToolbarMinus.height))) { propInfoDeleted = true; } EditorGUI.BeginChangeCheck(); GUILayout.Label(System.Enum.GetName(typeof(EffectEventType), propInfo.type)); GUILayout.Label("X", GUILayout.Width(10)); x = EditorGUILayout.FloatField("", propInfo.val.x, GUILayout.Width(30)); GUILayout.Label("Y", GUILayout.Width(10)); y = EditorGUILayout.FloatField("", propInfo.val.y, GUILayout.Width(30)); if (EditorGUI.EndChangeCheck()) { propInfo.val = new Vector2(x, y); EditorUtility.SetDirty(target); } if (propInfoDeleted) { info.propInfos.RemoveAt(j); --j; EditorUtility.SetDirty(target); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); } // Add Property EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); List <string> eventNameList = new List <string>(); eventNameList.Add("Add Property"); eventNameList.AddRange(System.Enum.GetNames(typeof(EffectEventType))); foreach (EffectInfo_Offset.PropInfo propInfo in info.propInfos) { int idx = eventNameList.IndexOf(System.Enum.GetName(typeof(EffectEventType), propInfo.type)); if (idx != -1) { eventNameList.RemoveAt(idx); } } int choice = EditorGUILayout.Popup(0, eventNameList.ToArray(), GUILayout.Width(100)); if (choice != 0) { EffectInfo_Offset.PropInfo propInfo = new EffectInfo_Offset.PropInfo(); propInfo.type = (EffectEventType)System.Enum.Parse(typeof(EffectEventType), eventNameList[choice]); propInfo.val = info.normal; info.propInfos.Add(propInfo); EditorUtility.SetDirty(target); } EditorGUILayout.EndHorizontal(); // if (infoDeleted) { _infos.RemoveAt(i); --i; EditorUtility.SetDirty(target); } } // new slot EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); exSpriteBase receiver = EditorGUILayout.ObjectField(null, typeof(exSpriteBase), true, GUILayout.Width(150)) as exSpriteBase; if (receiver != null) { EffectInfo_Offset info = new EffectInfo_Offset(); info.target = receiver; _infos.Add(info); EditorUtility.SetDirty(target); } GUILayout.Label(styles.iconToolbarPlus, GUILayout.Width(20)); EditorGUILayout.EndHorizontal(); GUILayout.EndVertical(); EditorGUILayout.EndHorizontal(); GUILayout.EndVertical(); GUILayout.Space(4f); GUILayout.EndHorizontal(); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void DicedUpdateBuffers(exSpriteBase _sprite, exTextureInfo _textureInfo, bool _useTextureOffset, Space _space, exList<Vector3> _vertices, exList<Vector2> _uvs, exList<int> _indices, int _vbIndex, int _ibIndex) { if (_textureInfo.isDiced == false) { SimpleUpdateBuffers(_sprite, _textureInfo, _useTextureOffset, _space, _vertices, _uvs, _indices, _vbIndex, _ibIndex); return; } //if (_vertices.Count == 0) { // return; //} if ((_sprite.updateFlags & exUpdateFlags.Vertex) != 0) { // get entire sprite SimpleUpdateVertexBuffer(_sprite, _textureInfo, _useTextureOffset, _space, _vertices, _vbIndex); SimpleVertexBufferToDiced(_sprite, _textureInfo, _vertices, _vbIndex); } if ((_sprite.updateFlags & exUpdateFlags.Index) != 0 && _indices != null) { /* dice index: 8 9 10 11 4 5 6 7 0 1 2 3 */ int i = _ibIndex; for (int v = _vbIndex; v < _vertices.Count; v += exMesh.QUAD_VERTEX_COUNT) { _indices.buffer[i++] = v; _indices.buffer[i++] = v + 1; _indices.buffer[i++] = v + 2; _indices.buffer[i++] = v + 2; _indices.buffer[i++] = v + 3; _indices.buffer[i++] = v; } } if ((_sprite.updateFlags & exUpdateFlags.UV) != 0) { Vector2 texelSize; if (_textureInfo.texture != null) { texelSize = _textureInfo.texture.texelSize; } else { texelSize = new Vector2(1.0f / _textureInfo.rawWidth, 1.0f / _textureInfo.rawHeight); } foreach (exTextureInfo.Dice dice in _textureInfo.dices) { if (dice.sizeType != exTextureInfo.DiceType.Empty) { Vector2 start = new Vector2(dice.x * texelSize.x, dice.y * texelSize.y); Vector2 end = new Vector2((dice.x + dice.rotatedWidth) * texelSize.x, (dice.y + dice.rotatedHeight) * texelSize.y); if ( dice.rotated ) { _uvs.buffer[_vbIndex++] = new Vector2(end.x, start.y); _uvs.buffer[_vbIndex++] = start; _uvs.buffer[_vbIndex++] = new Vector2(start.x, end.y); _uvs.buffer[_vbIndex++] = end; } else { _uvs.buffer[_vbIndex++] = start; _uvs.buffer[_vbIndex++] = new Vector2(start.x, end.y); _uvs.buffer[_vbIndex++] = end; _uvs.buffer[_vbIndex++] = new Vector2(end.x, start.y); } } } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void AddEffect_Offset( exSpriteBase _target, EffectEventType _type, exEase.Type _curveType, Vector2 _to, float _duration ) { exUIControl ctrl = GetComponent<exUIControl>(); if ( ctrl ) { EffectInfo_Offset info = new EffectInfo_Offset(); info.duration = _duration; info.target = _target; info.normal = _target.offset; info.curveType = _curveType; EffectInfo_Offset.PropInfo propInfo = new EffectInfo_Offset.PropInfo(); propInfo.type = _type; propInfo.val = _to; info.propInfos.Add(propInfo); EffectState_Offset state = new EffectState_Offset(); state.info = info; state.func = info.GetCurveFunction(); AddState_Offset( ctrl, state ); } }
public void UnregisterColor( exSpriteBase _sprite ) { for ( int i = 0; i < colorInfos.Count; ++i ) { if ( colorInfos[i].sprite == _sprite ) { colorInfos.RemoveAt(i); break; } } }
public void DestroyHitFX(exSpriteBase _fx) { _fx.enabled = false; hitFXPool.Return(_fx); }
// ------------------------------------------------------------------ /// \param _sprites the list of sprites to rebuild /// rebuild the listed sprites // ------------------------------------------------------------------ public static void RebuildSprites(exSpriteBase[] _sprites) { try { EditorUtility.DisplayProgressBar("Rebuild Scene Sprites...", "Rebuild Scene Sprites...", 0.5f); for (int i = 0; i < _sprites.Length; ++i) { exSpriteBase spBase = _sprites[i]; // DISABLE: it is too slow { // float progress = (float)i/(float)_sprites.Length; // EditorUtility.DisplayProgressBar( "Rebuild Scene Sprites...", // "Build Sprite " + spBase.gameObject.name, progress ); // } DISABLE end // if sprite if (spBase is exSprite) { exSprite sp = spBase as exSprite; exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(sp.textureGUID); exSpriteEditor.UpdateAtlas(sp, elInfo); Texture2D texture = null; if (sp.useAtlas == false) { texture = exEditorHelper.LoadAssetFromGUID <Texture2D>(sp.textureGUID); } sp.Build(texture); } #if !(EX2D_EVALUATE) // if sprite font if (spBase is exSpriteFont) { exSpriteFont spFont = spBase as exSpriteFont; spFont.Build(); } // if sprite border if (spBase is exSpriteBorder) { exSpriteBorder spBorder = spBase as exSpriteBorder; Texture2D texture = null; if (spBorder.guiBorder) { exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(spBorder.guiBorder.textureGUID); exSpriteBorderEditor.UpdateAtlas(spBorder, elInfo); if (spBorder.useAtlas == false) { texture = exEditorHelper.LoadAssetFromGUID <Texture2D>(spBorder.guiBorder.textureGUID); } } spBorder.Build(texture); } #endif // EX2D_EVALUATE } EditorUtility.ClearProgressBar(); } catch (System.Exception) { EditorUtility.ClearProgressBar(); throw; } }
public static void SetTextureInfo(exSpriteBase _sprite, ref exTextureInfo _ti, exTextureInfo _newTi, bool _useTextureOffset, exSpriteType _spriteType) { exTextureInfo old = _ti; _ti = _newTi; if (_newTi != null) { if (_newTi.texture == null) { Debug.LogWarning("invalid textureInfo"); } if (_spriteType == exSpriteType.Tiled) { if (old == null || ReferenceEquals(old, _newTi) || _newTi.width != old.width || _newTi.height != old.height) { (_sprite as exISprite).UpdateBufferSize (); _sprite.updateFlags |= exUpdateFlags.Vertex; // tile数量可能不变,但是间距可能会改变 } } else if (_spriteType == exSpriteType.Diced) { //if (_newTi.isDiced == false) { // Debug.LogWarning ("The texture info is not diced!"); //} (_sprite as exISprite).UpdateBufferSize (); _sprite.updateFlags |= exUpdateFlags.Vertex; } else { if (_sprite.customSize == false && (old == null || (_newTi.width != old.width || _newTi.height != old.height))) { _sprite.updateFlags |= exUpdateFlags.Vertex; } } if (_useTextureOffset) { _sprite.updateFlags |= exUpdateFlags.Vertex; } _sprite.updateFlags |= exUpdateFlags.UV; // 换了texture,UV也会重算,不换texture就更要改UV,否则没有换textureInfo的必要了。 if (old == null || ReferenceEquals(old.texture, _newTi.texture) == false) { // texture changed _sprite.updateFlags |= (exUpdateFlags.Vertex | exUpdateFlags.UV); (_sprite as exISprite).UpdateMaterial(); } } }
// ------------------------------------------------------------------ /// \param _sprite the in sprite /// \param _depthToCamera the depth to camera // ------------------------------------------------------------------ public void CalculatePixelPerfectScale ( exSpriteBase _sprite, float _depthToCamera ) { if ( (camera.orthographic && scale <= 0.0f) || ratio <= 0.0f ) CalculateScaleAndRatio (); float s = scale; if ( camera.orthographic == false ) s = ratio * _depthToCamera; _sprite.ppfScale = new Vector2( s, s ); }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Awake() { sprite = GetComponent <exSpriteBase>(); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void Return( exSpriteBase _fx ) { ++idx; hitFXs[idx] = _fx; }