cocos2d-x接入live2d3.0学习

1. 准备阶段

  1. 下载安装Python,安装cocos2d-x需要用到Python
  2. 下载live2d3.0的sdk(Cubism SDK for Native) https://live2d.github.io/
  3. 下载cocos2d-x源码 (cocos2d-x 3.16) https://cocos2d-x.org/download/version
  4. 如果要做android开发,还需要下载android ndk,android sdk,apache ant

2. 安装运行cocos2d

启动命令行,cd到cocos2d-x-3.16的解压目录,运行如下代码,我创建的是lua项目
cocos命令在cocos2d-x-3.16\tools\cocos2d-console\bin\目录下,可配置成环境变量,如果不配置环境变量的话,需要带路径使用

cd cocos2d-x-3.16
./setup.py
cocos new MyGame -p com.company.mygame -l lua -d D:\MyProject\
cd D:\MyProject\MyGame
cocos run -p win32

3. 编译运行-win32

将live2d3.0的sdk目录下的头文件、资源文件、lib库和示例代码拷贝到我们的项目中来

xcopy .\Cubism3SDK\Framework\src D:\MyProject\MyGame\frameworks\Cubism3.0\
xcopy .\Cubism3SDK\Core\include D:\MyProject\MyGame\frameworks\Cubism3.0\
xcopy .\Cubism3SDK\Core\lib D:\MyProject\MyGame\frameworks\Cubism3.0\
xcopy .\Samples\Cocos2d-x\Demo\Classes D:\MyProject\MyGame\frameworks\runtime-src\

给工程添加搜索目录、宏、依赖库搜索路径、依赖库
我用的vs2013 我电脑win32只能添加Cubism3.0\lib\windows\x86\120\Live2DCubismCore_MT.lib库才能编译通过
另外GL的编译宏按需要编译的平台添加,win32添加CSM_TARGET_WIN_GL即可

头文件搜索路径
$(ProjectDir)..\..\Cubism3.0\include;
$(ProjectDir)..\..\Cubism3.0\src;
$(EngineRoot)cocos\audio\include;
编译宏
CSM_TARGET_WIN_GL
_USE_MATH_DEFINES;
CC_ENABLE_CHIPMUNK_INTEGRATION=1;
库文件搜索路径
$(ProjectDir)..\..\Cubism3.0\lib\windows\x86\120;
依赖库
Live2DCubismCore_MTd.lib;

将下面目录的文件全部添加到工程里,注意,frameworks\Cubism3.0\src\Rendering目录下只需要添加OpenGL即可

D:\MyProject\MyGame\frameworks\Cubism3.0\src
D:\MyProject\MyGame\frameworks\Cubism3.0\include
D:\MyProject\MyGame\frameworks\runtime-src\Classes

另外还修改一丢丢代码

EventListenerCustom 加上命名空间 cocos2d::EventListenerCustom 
  • 编译运行示例-win32完

4.编译运行-Android

给工程添加搜索目录、宏、依赖库搜索路径、依赖库
打开frameworks\Cubism3.0\lib\android\目录,新建库的mk文件./Android.mk
没写过mk文件,我是参考cocos自带的mk文件写的

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := live2d_cubismcore_static
LOCAL_MODULE_FILENAME := Live2DCubismCore
ifeq ($(TARGET_ARCH_ABI),armeabi)
	LOCAL_SRC_FILES := armeabi-v7a/libLive2DCubismCore.a
else
	LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libLive2DCubismCore.a
endif
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../include
include $(PREBUILT_STATIC_LIBRARY)

打开frameworks/runtime-src/proj.android/jni/Application.mk,添加修改编译宏

#自定义的宏要加-D
APP_CPPFLAGS := -frtti -std=c++11 -fsigned-char\
-DCC_ENABLE_CHIPMUNK_INTEGRATION=1 \
-D_USE_MATH_DEFINES \
-DCSM_TARGET_ANDROID_ES2

打开"frameworks/runtime-src/proj.android/jni/Android.mk",添加编译文件和include头

#添加编译文件
LOCAL_SRC_FILES := \
../../Classes/AppDelegate.cpp \
../../Classes/LAppAllocator.cpp \
../../Classes/LAppDefine.cpp \
../../Classes/LAppLive2DManager.cpp \
../../Classes/LAppModel.cpp \
../../Classes/LAppPal.cpp \
../../Classes/LAppView.cpp \
../../Classes/SampleScene.cpp \
../../Classes/TouchManager.cpp \
../../../Cubism3.0/src/CubismDefaultParameterId.cpp \
../../../Cubism3.0/src/CubismFramework.cpp \
../../../Cubism3.0/src/CubismModelSettingJson.cpp \
../../../Cubism3.0/src/Effect/CubismBreath.cpp \
../../../Cubism3.0/src/Effect/CubismEyeBlink.cpp \
../../../Cubism3.0/src/Effect/CubismPose.cpp \
../../../Cubism3.0/src/Id/CubismId.cpp \
../../../Cubism3.0/src/Id/CubismIdManager.cpp \
../../../Cubism3.0/src/Math/CubismMath.cpp \
../../../Cubism3.0/src/Math/CubismMatrix44.cpp \
../../../Cubism3.0/src/Math/CubismModelMatrix.cpp \
../../../Cubism3.0/src/Math/CubismTargetPoint.cpp \
../../../Cubism3.0/src/Math/CubismVector2.cpp \
../../../Cubism3.0/src/Math/CubismViewMatrix.cpp \
../../../Cubism3.0/src/Model/CubismMoc.cpp \
../../../Cubism3.0/src/Model/CubismModel.cpp \
../../../Cubism3.0/src/Model/CubismModelUserData.cpp \
../../../Cubism3.0/src/Model/CubismModelUserDataJson.cpp \
../../../Cubism3.0/src/Model/CubismUserModel.cpp \
../../../Cubism3.0/src/Motion/ACubismMotion.cpp \
../../../Cubism3.0/src/Motion/CubismExpressionMotion.cpp \
../../../Cubism3.0/src/Motion/CubismMotion.cpp \
../../../Cubism3.0/src/Motion/CubismMotionJson.cpp \
../../../Cubism3.0/src/Motion/CubismMotionManager.cpp \
../../../Cubism3.0/src/Motion/CubismMotionQueueEntry.cpp \
../../../Cubism3.0/src/Motion/CubismMotionQueueManager.cpp \
../../../Cubism3.0/src/Physics/CubismPhysics.cpp \
../../../Cubism3.0/src/Physics/CubismPhysicsJson.cpp \
../../../Cubism3.0/src/Rendering/CubismRenderer.cpp \
../../../Cubism3.0/src/Rendering/OpenGL/CubismRenderer_OpenGLES2.cpp \
../../../Cubism3.0/src/Type/csmRectF.cpp \
../../../Cubism3.0/src/Type/csmString.cpp \
../../../Cubism3.0/src/Utils/CubismDebug.cpp \
../../../Cubism3.0/src/Utils/CubismJson.cpp \
../../../Cubism3.0/src/Utils/CubismString.cpp \
hellolua/main.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
#添加头文件搜索路径
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../Cubism3.0/src
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../Cubism3.0/include

依旧打开frameworks/runtime-src/proj.android/jni/Android.mk,添加引用库文件

LOCAL_STATIC_LIBRARIES := cocos2d_lua_static 
#添加live2d的引用库
LOCAL_STATIC_LIBRARIES += live2d_cubismcore_static
include $(BUILD_SHARED_LIBRARY)
$(call import-module,scripting/lua-bindings/proj.android)
#添加live2d库的Android.mk的搜索路径
$(call import-module,../../../Cubism3.0/lib/android)

编译apk

cd D:\MyProject\MyGame
cocos run -p android

编译运行示例-android完

5.编译运行-IOS

添加头文件和目标文件到项目中去
Build Setting -> User Header Search Paths

$(SRCROOT)/../../Cubism3.0/include
$(SRCROOT)/../../Cubism3.0/src

Build Setting -> Preprocessor Macros

_USE_MATH_DEFINES 
CSM_TARGET_IPHONE_ES2

General -> Linked Frameworks and Librarles
将Cubism3.0/lib/ios/Release-iphoneos/libLive2DCubismCore.a拖进去
Build Setting -> Library Search Paths

$(SRCROOT)/../../Cubism3.0/lib/ios/Debug-iphoneos

编译运行

你可能感兴趣的:(live2d)