{"id":5568,"date":"2024-01-08T10:58:34","date_gmt":"2024-01-08T02:58:34","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=5568"},"modified":"2024-01-08T10:58:34","modified_gmt":"2024-01-08T02:58:34","slug":"unity-%e5%8a%a8%e7%94%bb%e5%b8%a7%e9%87%87%e6%a0%b7","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=5568","title":{"rendered":"Unity  \u52a8\u753b\u5e27\u91c7\u6837"},"content":{"rendered":"\n<p>\u7f8e\u672f\u53ef\u4ee5\u5236\u4f5c\u4e00\u4e2a\u590d\u6742\u7684Animation\uff0c\u7a0b\u5e8f\u53ea\u9700\u8981\u8c03\u7528\u4e00\u4e0bSetAniProgress\uff0c\u6307\u5b9a\u8fdb\u5ea6\u6761\u5c31\u884c\u4e86<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using UnityEngine;\r\n\r\npublic class PlayFrame : MonoBehaviour\r\n{\r\n    public float rate = 0.35f;\r\n    public string clipName = \"Ani1\";\r\n    \r\n    public static int SetAniProgress(GameObject obj, string clipName, float progress)\r\n    {\r\n        if (obj == null)\r\n        {\r\n            return -1;\r\n        }\r\n\r\n        var aniObj = obj.GetComponent&lt;Animation>();\r\n        if (aniObj == null)\r\n        {\r\n            return -2;\r\n        }\r\n\r\n        var clip = aniObj.GetClip(clipName);\r\n        if (clip == null)\r\n        {\r\n            return -3;\r\n        }\r\n\r\n        var time = clip.length * progress;\r\n        clip.SampleAnimation(obj, time);\r\n        return 0;\r\n    }\r\n\r\n    void Update()\r\n    {\r\n        SetAniProgress(this.gameObject, clipName, rate);\r\n    }\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7f8e\u672f\u53ef\u4ee5\u5236\u4f5c\u4e00\u4e2a\u590d\u6742\u7684Animation\uff0c\u7a0b\u5e8f\u53ea\u9700\u8981\u8c03\u7528\u4e00\u4e0bSetAniProgress\uff0c\u6307\u5b9a\u8fdb\u5ea6\u6761\u5c31\u884c\u4e86<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/5568"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5568"}],"version-history":[{"count":1,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/5568\/revisions"}],"predecessor-version":[{"id":5569,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/5568\/revisions\/5569"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}