gpt4 book ai didi

android - Unity-强制Android硬件加速

转载 作者:行者123 更新时间:2023-12-01 18:09:44 26 4
gpt4 key购买 nike

我制作了一个使用后处理效果的 2D 游戏。因此,当后处理关闭时,游戏在我的旧平板电脑上运行良好,而当后处理打开时,游戏会卡顿,但我想探索我的可能性。

有没有办法在我的 Unity 项目中强制 Android 硬件加速?

编辑:我将此 list 放入项目的 android 文件夹中。

我做对了吗?性能无显着差异。

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
xmlns:tools="http://schemas.android.com/tools"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0">
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>

<application
android:hardwareAccelerated="true"
android:theme="@style/UnityThemeSelector"
android:icon="@drawable/app_icon"
android:label="@string/app_name">
<activity android:name="com.unity3d.player.UnityPlayerActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
</application>
</manifest>

编辑器截图:

enter image description here

最佳答案

程序员的答案仅在您没有任何其他具有自己的 Android list 的 Android 插件时才有效。这是万无一失的方法(Unity 2018.1+):

  1. 下载 Assets/Editor/ModifyUnityAndroidAppManifestSample.cs 脚本 here 并将其放置在项目中的同一位置。

  2. 将以下函数添加到 AndroidManifest 类中:

    internal void SetHardwareAccel(){
    GetActivityWithLaunchIntent().Attributes.Append(CreateAndroidAttribute("hardwareAccelerated", "true"));
    }
  3. androidManifest.SetHardwareAccel();中调用OnPostGenerateGradleAndroidProject(string basePath)

编辑:如果上面的链接中断或者您讨厌执行多个步骤(我们都不是),这里是您可以粘贴到 Assets/Editor/ModifyUnityAndroidAppManifestSample.cs 的类(包括上面的方法和调用):

using System.IO;
using System.Text;
using System.Xml;
using UnityEditor.Android;

public class ModifyUnityAndroidAppManifestSample : IPostGenerateGradleAndroidProject
{

public void OnPostGenerateGradleAndroidProject(string basePath)
{
// If needed, add condition checks on whether you need to run the modification routine.
// For example, specific configuration/app options enabled

var androidManifest = new AndroidManifest(GetManifestPath(basePath));

androidManifest.SetHardwareAccel();

// Add your XML manipulation routines

androidManifest.Save();
}

public int callbackOrder { get { return 1; } }

private string _manifestFilePath;

private string GetManifestPath(string basePath)
{
if (string.IsNullOrEmpty(_manifestFilePath))
{
var pathBuilder = new StringBuilder(basePath);
pathBuilder.Append(Path.DirectorySeparatorChar).Append("src");
pathBuilder.Append(Path.DirectorySeparatorChar).Append("main");
pathBuilder.Append(Path.DirectorySeparatorChar).Append("AndroidManifest.xml");
_manifestFilePath = pathBuilder.ToString();
}
return _manifestFilePath;
}
}


internal class AndroidXmlDocument : XmlDocument
{
private string m_Path;
protected XmlNamespaceManager nsMgr;
public readonly string AndroidXmlNamespace = "http://schemas.android.com/apk/res/android";
public AndroidXmlDocument(string path)
{
m_Path = path;
using (var reader = new XmlTextReader(m_Path))
{
reader.Read();
Load(reader);
}
nsMgr = new XmlNamespaceManager(NameTable);
nsMgr.AddNamespace("android", AndroidXmlNamespace);
}

public string Save()
{
return SaveAs(m_Path);
}

public string SaveAs(string path)
{
using (var writer = new XmlTextWriter(path, new UTF8Encoding(false)))
{
writer.Formatting = Formatting.Indented;
Save(writer);
}
return path;
}
}


internal class AndroidManifest : AndroidXmlDocument
{
private readonly XmlElement ApplicationElement;

public AndroidManifest(string path) : base(path)
{
ApplicationElement = SelectSingleNode("/manifest/application") as XmlElement;
}

private XmlAttribute CreateAndroidAttribute(string key, string value)
{
XmlAttribute attr = CreateAttribute("android", key, AndroidXmlNamespace);
attr.Value = value;
return attr;
}

internal XmlNode GetActivityWithLaunchIntent()
{
return SelectSingleNode("/manifest/application/activity[intent-filter/action/@android:name='android.intent.action.MAIN' and " +
"intent-filter/category/@android:name='android.intent.category.LAUNCHER']", nsMgr);
}

internal void SetApplicationTheme(string appTheme)
{
ApplicationElement.Attributes.Append(CreateAndroidAttribute("theme", appTheme));
}

internal void SetStartingActivityName(string activityName)
{
GetActivityWithLaunchIntent().Attributes.Append(CreateAndroidAttribute("name", activityName));
}
internal void SetHardwareAccel(){
GetActivityWithLaunchIntent().Attributes.Append(CreateAndroidAttribute("hardwareAccelerated", "true"));
}
}

关于android - Unity-强制Android硬件加速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45820447/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com