Unity-Reduce the size of your animation files by reducing precision

Source: Internet
Author: User
Tags python script

Animation is an animated file in unity, and the main content is composed of key frame data. By adjusting Unity's resource serialization to text, you can view the animation file in text mode.
You can set the resource serialization mode by opening the Editor Settings window with the menu item, Project Settings, editor:

Shows my animation of a cube, which contains several keyframes that adjust the coordinates and direction of rotation of the cube:

Open the animation file as text, part of the following:

The serialization format of the animation file is not within our scope of discussion, the main discussion in this article is to reduce the size of the animation file by reducing the precision. By looking at the animated file, we find that unity uses a higher floating-point precision when serializing the animation file, and can be many bits behind the decimal point. Therefore, by reducing the precision we can reduce the size of the animation file.
Here we use Python script to implement this function, the logic of processing is as follows:
1. Read each line in the animation file
2. For each line that is read, the line break at the end of the row is removed
3. Use "spaces" as separators to separate line contents
4. For each separated content, use a regular expression query to include floating-point data
5. If you include floating-point data, use rounding to keep the decimal point 3 bits. Writes directly to the output file if it does not contain floating-point data

Here is the Python code, which you can adjust as needed:

Import reanimfile = open ("Move.anim") OutputFile = open ("Newmove.anim", "w", newline= ' \ n ') for L in Animfile.readlines (): c0/># reads each line in the file    , lines = L.rstrip ()   # for each line that is read, the newline character at the end of the line is stripped    words = Line.split (")     # uses" space "as the delimiter, separating the line contents    for word in words:        match = Re.match ("-?\d+\.\d+", Word)    # If you use a regular expression query to include floating-point data for each content that is delimited,        The decimal point is retained by rounding 3 bits. If it does not contain floating-point data, it is written directly to the output file if        match:            value = Match.group (0)            Floatvalue = float (value)            Outputfile.write (Word.replace (value, str (Round (Floatvalue, 3)        ))) else:            outputfile.write (word)        if Word! = Words[-1]:            outputfile.write (')    outputfile.write (' \ n ') )

By reducing the precision adjustment, the animation files are as follows:

By reducing the accuracy of the adjustment, we reduce the size of 19.2KB animation files to 18.3KB. It seems trivial, because this animation file contains only 13 frames of animation data, the saving is very objective for hundreds of frames of animated files in real projects. For example, the 133-second animation file reduces the size from 8MB to 3MB.

Unity-Reduce the size of your animation files by reducing precision

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.