You need to enable JavaScript to run this app.
导航
Unity SDK 集成开发指南
最近更新时间:2024.08.22 15:06:10首次发布时间:2024.08.22 15:06:10

插件下载
RangersPlugin_v0.0.1.unitypackage
13.40KB

一、导入插件

图片
图片

二、Unity场景开发

1. 新建场景,添加按钮

图片

2. 绑定脚本

图片

3. 绑定Onclick方法

图片

三、Build Setting

图片

四、PlayerSettings

  • Other Settings设置包名和版本号

图片

  • Publish Settings设置mainfest、base gradle和Main gradle

图片

五、Android配置

1. mainfest文件配置

修改生成好的AndroidMainfest.xml文件,添加自定义的application

<?xml version="1.0" encoding="utf-8"?>
<!-- GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN-->
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unity3d.player"
    xmlns:tools="http://schemas.android.com/tools">
    <application android:name=".TheApplication">
        <activity android:name="com.unity3d.player.UnityPlayerActivity"
                  android:theme="@style/UnityThemeSelector">
            <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>

2. Base Gradle文件配置仓库

// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN

allprojects {
    buildscript {
        repositories {**ARTIFACTORYREPOSITORY**
            maven {
                url 'https://artifact.bytedance.com/repository/Volcengine/'
            }
            google()
            jcenter()

        }

        dependencies {
            // If you are changing the Android Gradle Plugin version, make sure it is compatible with the Gradle version preinstalled with Unity
                        // See which Gradle version is preinstalled with Unity here https://docs.unity3d.com/Manual/android-gradle-overview.html
                        // See official Gradle and Android Gradle Plugin compatibility table here https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
                        // To specify a custom Gradle version in Unity, go do "Preferences > External Tools", uncheck "Gradle Installed with Unity (recommended)" and specify a path to a custom Gradle version
            classpath 'com.android.tools.build:gradle:3.4.0'
            **BUILD_SCRIPT_DEPS**
        }
    }

    repositories {**ARTIFACTORYREPOSITORY**
        maven {
    url 'https://artifact.bytedance.com/repository/Volcengine/'
                    }
        google()
        jcenter()
        flatDir {
            dirs "${project(':unityLibrary').projectDir}/libs"
                }

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

3. Mian gradle文件配置依赖

// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
buildscript {
   repositories {
        mavenCentral()
      google()
      jcenter()

      }

   dependencies {
            classpath 'com.android.tools.build:gradle:3.4.0'
      }
}

allprojects {
   repositories {
        mavenCentral()

      google()
      jcenter()
      flatDir {
        dirs 'libs'
            }
      }
}

apply plugin: 'com.android.library'

dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61'
   implementation 'com.bytedance.frameworks.baselib:utility:2.0.11'
   implementation 'com.bytedance.frameworks:encryptor:0.0.9-rc.2-private'
   implementation 'com.bytedance.applog:RangersAppLog-Lite-cn:6.3.0'

   **DEPS**}

android {
   compileSdkVersion **APIVERSION**
   buildToolsVersion '**BUILDTOOLS**'

   defaultConfig {
consumerProguardFiles 'proguard-unity.txt'**USER_PROGUARD**
      minSdkVersion **MINSDKVERSION**
      targetSdkVersion **TARGETSDKVERSION**

      ndk {
            abiFilters **ABIFILTERS**
        }
      versionCode    **VERSIONCODE**
      versionName    '**VERSIONNAME**'
   }

   lintOptions {
      abortOnError false
      }

   aaptOptions {
      noCompress '.unity3d', '.ress', '.resource', '.obb'**STREAMING_ASSETS**
   }

**SIGN**
   buildTypes {
      debug {
         minifyEnabled **MINIFY_DEBUG**
         useProguard **PROGUARD_DEBUG**
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD**
         jniDebuggable true
            }
      release {
         minifyEnabled **MINIFY_RELEASE**
         useProguard **PROGUARD_RELEASE**
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD**
         **SIGNCONFIG**
      }
   }
**PACKAGING_OPTIONS**

}

**SOURCE_BUILD_SETUP**

4. The Application代码

图片

/* 初始化开始 */
final  InitConfig config = new  InitConfig("175128", "cuckoo"); // appid和渠道,appid如不清楚请联系客户成功经理,注意第二个参数 channel 不能为空

config.setUriConfig(UriConstants.DEFAULT);//上报地址
config.setAbEnable(true); // 开启 AB 测试

// 是否在控制台输出日志,可用于观察用户行为日志上报情况,上线之前可去掉
config.setLogger(new  ILogger() {
    @Override
    public void  log(String s, Throwable throwable) {
        Log.d("AppLog------->: ", ""  + s);
    }
});

// 加密开关,SDK 5.5.1 及以上版本支持,false 为关闭加密,上线前建议设置为 true
AppLog.setEncryptAndCompress(false);
config.setAutoStart(true);
AppLog.init(this, config);
/* 初始化结束 */

六、iOS配置

在Build Setting 平台切换到iOS,在导出工程中找到UnityAppController.mm文件,在didFinishLaunchingWithOptions函数,加入初始化代码

  • 使用xcode打开工程,执行cocopods相关命令:
pod setup
pod init
  • Podfile配置:
# Uncomment the next line to define a global platform for your project
 platform :ios, '9.0'
source 'https://github.com/volcengine/volcengine-specs.git'
target 'Unity-iPhone' do
 # Comment the next line if you don't want to use dynamic frameworks
 use_frameworks!

 # Pods for Unity-iPhone

 target 'Unity-iPhone Tests' do
  inherit! :search_paths
  # Pods for testing
 end

end

target 'UnityFramework' do
 # Comment the next line if you don't want to use dynamic frameworks
 use_frameworks!
 # applog的版本
 pod 'RangersAppLog', '6.2.3',
   :subspecs => [
    'Host/CN',
    'Core',
    'Unique',
    'Log',
    'CBridge'
   ]
end
  • 安装依赖
pod install --repo-update
  • 初始化代码,框架默认在UnityAppController.mm文件中,找到didFinishLaunchingWithOptions方法,在return YES的代码前面添加初始化代码
#import <RangersAppLog/BDAutoTrack.h>

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
//Init RangersAppLog
 BDAutoTrackConfig *config = [BDAutoTrackConfig configWithAppID:@"123456" launchOptions:launchOptions];
 //debug日志开关,正式版本修改修改为NO
 config.showDebugLog = YES;
 config.logger = ^(NSString * _Nullable log) {
 NSLog(@"%@", log);
 };
 config.appName = @"Unity test app";
 //加密开关,正式版本修改修改为YES
 config.logNeedEncrypt = NO;
 [BDAutoTrack startTrackWithConfig:config];
 return YES;
}

七、日志查看

1. Android

  • 安装插件,右击package,选择view in Package Manager

图片

  • 选择unity_registry,选择按照Android Logcat

图片

  • 插上 手机,在Build Seting正确的情况下,cmd+B就可以编译并运行工程了

图片

2. iOS

在xCode中查看
图片

八、插件代码示例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class lay_playGame : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    public void EventV3()
    {
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("aaa", "111");
            param.Add("bbb", "222");
            RangersAppLog.Wrapper.RangersAppLog.EventV3("test", param);
            print("事件上报");
    }

    public void SetCustomHeaderInfo()
    {
            RangersAppLog.Wrapper.RangersAppLog.SetCustomHeaderInfo("u_h_name", "my_first_unity");
            print("设置消息头");
    }

    public void RemoveCustomHeaderInfo()
    {
            RangersAppLog.Wrapper.RangersAppLog.RemoveCustomHeaderInfo("u_h_name");
            print("移除消息头");
    }

    public void ProfileSet()
    {
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("u_p_age", 100);

            RangersAppLog.Wrapper.RangersAppLog.ProfileSet(param);
            print("调用ProfileSet");
    }

    public void ProfileSetOnce()
    {
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("u_p_name", "i am a unity project");

            RangersAppLog.Wrapper.RangersAppLog.ProfileSetOnce(param);
            print("调用ProfileSetOnce");

    }

    public void ProfileUnset()
    {
            RangersAppLog.Wrapper.RangersAppLog.ProfileUnset("u_p_name");
            RangersAppLog.Wrapper.RangersAppLog.ProfileUnset("u_p_age");

    }

    public void ProfileIncrement()
    {
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("u_p_age", 1);

            RangersAppLog.Wrapper.RangersAppLog.ProfileIncrement(param);
    }

    public void ProfileAppend()
    {
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("u_p_fav", "游泳");
            RangersAppLog.Wrapper.RangersAppLog.ProfileAppend(param);
            print("调用ProfileAppend");

    }
    public void GetAllABConfig()
    {
            Dictionary<string, object> config = RangersAppLog.Wrapper.RangersAppLog.GetAllAbTestConfig();
            foreach (var item in config)
            {

                    print("abtest,key is " + item.Key + " value is " + ((Dictionary<string, object>)item.Value)["val"]);

                    object value = RangersAppLog.Wrapper.RangersAppLog.GetABTestConfigValueForKey(item.Key, "default");
                    Type type = value.GetType();

                    print("type is" + type.Name);

                    print("abtest,曝光" + item.Key + "value is " + (string)value);

            }
            print("调用GetAllAbTestConfigs");

    }
    public void GetAbSdkVersion()
    {
            string version = RangersAppLog.Wrapper.RangersAppLog.GetAbSdkVersion();
            print("版本号:" + version);
    }

    public void SetExternalVersion()
    {
            RangersAppLog.Wrapper.RangersAppLog.SetExternalAbVersion("100,101");
            print("设置SetExternalAbVersion");
    }

    public void ChangeUser()
    {
            string[] malePetNames = { "Rufus", "Bear", "Dakota", "Fido",
                                              "Vanya", "Samuel", "Koani", "Volodya",
                                              "Prince", "Yiska" };

            System.Random rnd = new System.Random();

            int index = rnd.Next(malePetNames.Length);
            string user = malePetNames[index];
            RangersAppLog.Wrapper.RangersAppLog.SetUserUniqueID(user);
            print("切换用户" + user);

    }

    public void Flush()
    {
            RangersAppLog.Wrapper.RangersAppLog.Flush();
            print("立即推送");
    }
    public void GetSDKLogInfo()
    {
            string aid = RangersAppLog.Wrapper.RangersAppLog.GetAid();
            string did = RangersAppLog.Wrapper.RangersAppLog.GetDeviceId();
            string iid = RangersAppLog.Wrapper.RangersAppLog.GetInstallId();
            string uuid = RangersAppLog.Wrapper.RangersAppLog.GetUserUniqueID();

            print(String.Format("aid is {0},did is {1},iid is {2},uuid is {3}", aid, did, iid, uuid));
    }
}