{"id":2459,"date":"2023-05-20T13:54:06","date_gmt":"2023-05-20T13:54:06","guid":{"rendered":"http:\/\/blog.zhukunqian.com\/?p=2459"},"modified":"2023-05-20T13:54:06","modified_gmt":"2023-05-20T13:54:06","slug":"iphone%e6%89%8b%e6%9c%ba%e5%9b%be%e6%a0%87%e6%8a%96%e5%8a%a8","status":"publish","type":"post","link":"https:\/\/blog.zhukunqian.com\/?p=2459","title":{"rendered":"iPhone\u624b\u673a\u56fe\u6807\u6296\u52a8"},"content":{"rendered":"\n<p>\u6700\u8fd1\u770b\u4e86iphone\u624b\u673a\u4e0a\u7684\u56fe\u6807\u6296\u52a8\u7b97\u6cd5\uff0c\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u73b0<\/p>\n\n\n\n<p><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<pre class=\"wp-block-code\">&lt;code&gt;using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\n\npublic class RotationAnimation : MonoBehaviour\n{\n    \/\/ Start is called before the first frame update\n    public float duration = 0.25f;\n    public float displacement = 1.0f;\n    public float degreesRotation = 2.0f;\n\n    public bool play = false;\n\n    public bool enablePos;\n    public bool enableRotation;\n\n  \n    public bool useXY = false;\n    public bool useXZ = true;\n    public bool useYZ = false;\n\n    private Position position;\n    private Rotation rotation;\n\n    private void Awake()\n    {\n \n        Init();\n\n        StartPlay();\n    }\n\n    public void StartPlay()\n    {\n        play = true;\n\n        position.startTime = Time.realtimeSinceStartup;\n        rotation.startTime = Time.realtimeSinceStartup;\n    }\n    public void StopPlay()\n    {\n        play = false;\n        transform.localPosition = Vector3.zero;\n        transform.localRotation = Quaternion.Euler(transform.localRotation.x, 0f, transform.localRotation.z);\n    }\n\n    public void  Update()\n    {\n\n        if (play)\n        {\n            if (enablePos)\n            {\n                position.Update();\n            }\n            if (enableRotation)\n            {\n                rotation.Update();\n            }\n        }\n    }\n\n \n\n    public void Init()\n    {\n        var negativeDisplacement = -1.0f * displacement;\n\n        position = new Position();\n        position.transform = transform;\n        position.duration = duration;\n        position.positions = new Vector2&amp;#91;] { \n            new Vector2(negativeDisplacement,negativeDisplacement),\n            new Vector2(0,0),\n            new Vector2(negativeDisplacement,0),\n            new Vector2(0,negativeDisplacement),\n            new Vector2(negativeDisplacement,negativeDisplacement),\n        };\n        position.useXY = useXY;\n        position.useXZ = useXZ;\n        position.useYZ = useYZ;\n        \n\n        rotation = new Rotation();\n        rotation.transform = transform;\n        rotation.duration = duration;\n        rotation.rotations = new float&amp;#91;] {\n         -1.0f * degreesRotation ,\n         degreesRotation ,\n        -1.0f * degreesRotation \n        };\n        rotation.useXY = useXY;\n        rotation.useXZ = useXZ;\n        rotation.useYZ = useYZ;\n    }\n\n    class Position\n    {\n        public float startTime;\n        public float delayTime;\n        public float duration;\n        public Vector2&amp;#91;] positions;\n        public Transform transform;\n        public bool useXZ = true;\n        public bool useXY = false;\n        public bool useYZ = false;\n\n        public void Update()\n        {\n            \n            \/\/ \u7ebf\u6027\u63d2\u503c\n            float time = Time.realtimeSinceStartup - startTime;\n            if (time &amp;lt;= delayTime)\n            {\n                return;\n            }\n\n            int rate = (int)(time \/ duration);\n            float lerp = (time - rate * duration)\/duration;\n            int index = rate % (positions.Length-1);\n            var startPos = positions&amp;#91;index];\n            var endPos = positions&amp;#91;index + 1];\n            \/\/Debug.Log(&quot;index: &quot; + index);\n\n            Vector2 pos = Vector2.Lerp(startPos, endPos,lerp);\n            if (useXZ)\n            {\n                transform.localPosition = new Vector3(pos.x, transform.localPosition.y, pos.y);\n            }else if (useXY)\n            {\n                transform.localPosition = new Vector3(pos.x, pos.y, transform.localPosition.z);\n            }\n            else if (useYZ)\n            {\n                transform.localPosition = new Vector3(transform.localPosition.x, pos.x,  pos.y);\n            }\n        }\n    }\n    class Rotation\n    {\n        public float startTime;\n        public float delayTime;\n        public float duration;\n        public float&amp;#91;] rotations;\n        public Transform transform;\n        public bool useXZ = true;\n        public bool useXY = false;\n        public bool useYZ = false;\n\n        public void Update()\n        {\n            \/\/ \u7ebf\u6027\u63d2\u503c\n            float time = Time.realtimeSinceStartup - startTime;\n            if (time &amp;lt;= delayTime)\n            {\n                return;\n            }\n\n            int rate = (int)(time \/ duration);\n            float lerp = (time - rate * duration)\/duration;\n            int index = rate % (rotations.Length - 1);\n            var start = rotations&amp;#91;index];\n            var end = rotations&amp;#91;index + 1];\n\n            float rotation = Mathf.Lerp(start, end, lerp);\n\n            \/\/Debug.Log(start + &quot; &quot; + end +  &quot; value:&quot;+rotation+&quot; lerp:&quot; + lerp+&quot; index:&quot;+index);\n            if (useXZ)\n            {\n                transform.localRotation = Quaternion.Euler(transform.localRotation.x, rotation, transform.localRotation.z);\n            }else if (useXY)\n            {\n                transform.localRotation = Quaternion.Euler(transform.localRotation.x,transform.localRotation.y, rotation);\n            }\n            else if (useYZ)\n            {\n                transform.localRotation = Quaternion.Euler(rotation,transform.localRotation.y, transform.localRotation.z);\n            }\n        }\n    }\n\n \n    \n}\n&lt;\/code&gt;<\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u770b\u4e86iphone\u624b\u673a\u4e0a\u7684\u56fe\u6807\u6296\u52a8\u7b97\u6cd5\uff0c\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u73b0<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/2459"}],"collection":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2459"}],"version-history":[{"count":1,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/2459\/revisions"}],"predecessor-version":[{"id":2460,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/2459\/revisions\/2460"}],"wp:attachment":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}